Skip to content

Commit

Permalink
feat: DB connection pool sizing (#2779)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored Sep 23, 2024
1 parent 73462c6 commit eff0336
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
13 changes: 13 additions & 0 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
ftlmaps "github.com/TBD54566975/ftl/internal/maps"
"github.com/TBD54566975/ftl/internal/model"
"github.com/TBD54566975/ftl/internal/modulecontext"
internalobservability "github.com/TBD54566975/ftl/internal/observability"
ftlreflect "github.com/TBD54566975/ftl/internal/reflect"
"github.com/TBD54566975/ftl/internal/rpc"
"github.com/TBD54566975/ftl/internal/rpc/headers"
Expand Down Expand Up @@ -100,6 +101,8 @@ type Config struct {
EventLogRetention *time.Duration `help:"Delete call logs after this time period. 0 to disable" env:"FTL_EVENT_LOG_RETENTION" default:"24h"`
ArtefactChunkSize int `help:"Size of each chunk streamed to the client." default:"1048576"`
KMSURI *string `help:"URI for KMS key e.g. with fake-kms:// or aws-kms://arn:aws:kms:ap-southeast-2:12345:key/0000-1111" env:"FTL_KMS_URI"`
MaxOpenDBConnections int `help:"Maximum number of database connections." default:"20" env:"FTL_MAX_OPEN_DB_CONNECTIONS"`
MaxIdleDBConnections int `help:"Maximum number of idle database connections." default:"20" env:"FTL_MAX_IDLE_DB_CONNECTIONS"`
CommonConfig
}

Expand All @@ -112,6 +115,16 @@ func (c *Config) SetDefaults() {
}
}

func (c *Config) OpenDBAndInstrument() (*sql.DB, error) {
conn, err := internalobservability.OpenDBAndInstrument(c.DSN)
if err != nil {
return nil, fmt.Errorf("failed to open DB connection: %w", err)
}
conn.SetMaxIdleConns(c.MaxIdleDBConnections)
conn.SetMaxOpenConns(c.MaxOpenDBConnections)
return conn, nil
}

// Start the Controller. Blocks until the context is cancelled.
func Start(ctx context.Context, config Config, runnerScaling scaling.RunnerScaling, conn *sql.DB, devel bool) error {
config.SetDefaults()
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/sql/databasetesting/devel.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func CreateForDevel(ctx context.Context, dsn string, recreate bool) (*stdsql.DB,
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
realConn.SetMaxIdleConns(20)
realConn.SetMaxOpenConns(20)
// Reset transient state in the database to a clean state for development purposes.
// This includes things like resetting the state of async calls, leases,
// controller/runner registration, etc. but not anything more.
Expand Down
2 changes: 1 addition & 1 deletion cmd/ftl-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
kctx.FatalIfErrorf(err, "failed to initialize observability")

// The FTL controller currently only supports DB as a cf provider/resolver.
conn, err := observability.OpenDBAndInstrument(cli.ControllerConfig.DSN)
conn, err := cli.ControllerConfig.OpenDBAndInstrument()
kctx.FatalIfErrorf(err)

dal := dbleaser.NewDatabaseLeaser(conn)
Expand Down
3 changes: 1 addition & 2 deletions frontend/cli/cmd_box_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/TBD54566975/ftl/internal/buildengine"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
"github.com/TBD54566975/ftl/internal/observability"
"github.com/TBD54566975/ftl/internal/projectconfig"
"github.com/TBD54566975/ftl/internal/rpc"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func (b *boxRunCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
}

// Bring up the DB connection and DAL.
conn, err := observability.OpenDBAndInstrument(config.DSN)
conn, err := config.OpenDBAndInstrument()
if err != nil {
return fmt.Errorf("failed to bring up DB connection: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/cli/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, ini
controllerCtx = manager.ContextWithSecrets(controllerCtx, sm)

// Bring up the DB connection and DAL.
conn, err := observability.OpenDBAndInstrument(config.DSN)
conn, err := config.OpenDBAndInstrument()
if err != nil {
return fmt.Errorf("failed to bring up DB connection: %w", err)
}
Expand Down

0 comments on commit eff0336

Please sign in to comment.