Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: increase pg pool connection idle timeout #1349

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/functions-runtime/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ function getDialect() {
return new PostgresDialect({
pool: new InstrumentedPool({
Client: InstrumentedClient,
// Increased idle time before closing a connection in the local pool (from 10s default).
// Establising a new connection on (almost) every functions query can be expensive, so this
// will reduce having to open connections as regularly. https://node-postgres.com/apis/pool
//
// NOTE: We should consider setting this to 0 (i.e. never pool locally) and open and close
// connections with each invocation. This is because the freeze/thaw nature of lambdas can cause problems
// with long-lived connections - see https://github.com/brianc/node-postgres/issues/2718
// Once we're "fully regional" this should not be a performance problem anymore.
//
// Although I doubt we will run into these freeze/thaw issues if idleTimeoutMillis is always shorter than the
// time is takes for a lambda to freeze (which is not a constant, but could be as short as several minutes,
// https://www.pluralsight.com/resources/blog/cloud/how-long-does-aws-lambda-keep-your-idle-functions-around-before-a-cold-start)
idleTimeoutMillis: 120000,
connectionString: mustEnv("KEEL_DB_CONN"),
}),
});
Expand Down
7 changes: 0 additions & 7 deletions runtime/actions/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ func Create(scope *Scope, input map[string]any) (res map[string]any, err error)
case !canResolveEarly:
err = database.Transaction(scope.Context, func(ctx context.Context) error {
scope := scope.WithContext(ctx)
query := NewQuery(scope.Model)

// Generate the SQL statement
statement, err := GenerateCreateStatement(query, scope, input)
if err != nil {
return err
}

// Execute database request, expecting a single result
res, err = statement.ExecuteToSingle(scope.Context)
Expand Down
3 changes: 1 addition & 2 deletions runtime/actions/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ func Get(scope *Scope, input map[string]any) (map[string]any, error) {
return nil, common.NewPermissionError()
}

query := NewQuery(scope.Model)

// Generate the SQL statement
query := NewQuery(scope.Model)
statement, err := GenerateGetStatement(query, scope, input)
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions runtime/actions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ func List(scope *Scope, input map[string]any) (map[string]any, error) {
return nil, common.NewPermissionError()
}

query := NewQuery(scope.Model)

// Generate the SQL statement.
query := NewQuery(scope.Model)
statement, page, err := GenerateListStatement(query, scope, input)
if err != nil {
return nil, err
Expand Down
Loading