Check PostgreSQL connection count

This commit is contained in:
Neil Alexander 2022-10-04 12:01:37 +01:00
parent a767102f8a
commit 627a408e98
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -37,15 +37,31 @@ func Open(dbProperties *config.DatabaseOptions, writer Writer) (*sql.DB, error)
return nil, err return nil, err
} }
if driverName != "sqlite3" { if driverName != "sqlite3" {
logrus.WithFields(logrus.Fields{ logger := logrus.WithFields(logrus.Fields{
"MaxOpenConns": dbProperties.MaxOpenConns(), "max_open_conns": dbProperties.MaxOpenConns(),
"MaxIdleConns": dbProperties.MaxIdleConns(), "max_idle_conns": dbProperties.MaxIdleConns(),
"ConnMaxLifetime": dbProperties.ConnMaxLifetime(), "conn_max_lifetime": dbProperties.ConnMaxLifetime(),
"dataSourceName": regexp.MustCompile(`://[^@]*@`).ReplaceAllLiteralString(dsn, "://"), "data_source_name": regexp.MustCompile(`://[^@]*@`).ReplaceAllLiteralString(dsn, "://"),
}).Debug("Setting DB connection limits") })
logger.Debug("Setting DB connection limits")
db.SetMaxOpenConns(dbProperties.MaxOpenConns()) db.SetMaxOpenConns(dbProperties.MaxOpenConns())
db.SetMaxIdleConns(dbProperties.MaxIdleConns()) db.SetMaxIdleConns(dbProperties.MaxIdleConns())
db.SetConnMaxLifetime(dbProperties.ConnMaxLifetime()) db.SetConnMaxLifetime(dbProperties.ConnMaxLifetime())
if driverName == "postgres" {
// Perform a quick sanity check if possible that we aren't trying to use more database
// connections than PostgreSQL is willing to give us.
var max, reserved int
if err := db.QueryRow("SELECT setting::integer FROM pg_settings WHERE name='max_connections';").Scan(&max); err != nil {
return nil, fmt.Errorf("failed to find maximum connections: %w", err)
}
if err := db.QueryRow("SELECT setting::integer FROM pg_settings WHERE name='superuser_reserved_connections';").Scan(&reserved); err != nil {
return nil, fmt.Errorf("failed to find reserved connections: %w", err)
}
if configured, allowed := dbProperties.MaxOpenConns(), max-reserved; configured > allowed {
return nil, fmt.Errorf("the max_open_conns is greater than the %d non-superuser connections that PostgreSQL is configured to allow", allowed)
}
}
} }
return db, nil return db, nil
} }