mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Check PostgreSQL connection count
This commit is contained in:
parent
a767102f8a
commit
627a408e98
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue