Skip to content

Commit 089659f

Browse files
authored
fix: Move SQL connection limits to initialization (#4981)
The connection limit wasn't being applied to pubsub, which would overload the server if a ton of logs were being published at once. This should fix it, and improve scale a lot!
1 parent 90c34b7 commit 089659f

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

cli/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,24 @@ func Server(vip *viper.Viper, newAPI func(context.Context, *coderd.Options) (*co
447447
if err != nil {
448448
return xerrors.Errorf("migrate up: %w", err)
449449
}
450+
// The default is 0 but the request will fail with a 500 if the DB
451+
// cannot accept new connections, so we try to limit that here.
452+
// Requests will wait for a new connection instead of a hard error
453+
// if a limit is set.
454+
sqlDB.SetMaxOpenConns(10)
455+
// Allow a max of 3 idle connections at a time. Lower values end up
456+
// creating a lot of connection churn. Since each connection uses about
457+
// 10MB of memory, we're allocating 30MB to Postgres connections per
458+
// replica, but is better than causing Postgres to spawn a thread 15-20
459+
// times/sec. PGBouncer's transaction pooling is not the greatest so
460+
// it's not optimal for us to deploy.
461+
//
462+
// This was set to 10 before we started doing HA deployments, but 3 was
463+
// later determined to be a better middle ground as to not use up all
464+
// of PGs default connection limit while simultaneously avoiding a lot
465+
// of connection churn.
466+
sqlDB.SetMaxIdleConns(3)
467+
450468
options.Database = database.New(sqlDB)
451469
options.Pubsub, err = database.NewPubsub(ctx, sqlDB, cfg.PostgresURL.Value)
452470
if err != nil {

coderd/database/db.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,6 @@ type DBTX interface {
4242
// New creates a new database store using a SQL database connection.
4343
func New(sdb *sql.DB) Store {
4444
dbx := sqlx.NewDb(sdb, "postgres")
45-
46-
// The default is 0 but the request will fail with a 500 if the DB
47-
// cannot accept new connections, so we try to limit that here.
48-
// Requests will wait for a new connection instead of a hard error
49-
// if a limit is set.
50-
dbx.SetMaxOpenConns(40)
51-
// Allow a max of 3 idle connections at a time. Lower values end up
52-
// creating a lot of connection churn. Since each connection uses about
53-
// 10MB of memory, we're allocating 30MB to Postgres connections per
54-
// replica, but is better than causing Postgres to spawn a thread 15-20
55-
// times/sec. PGBouncer's transaction pooling is not the greatest so
56-
// it's not optimal for us to deploy.
57-
//
58-
// This was set to 10 before we started doing HA deployments, but 3 was
59-
// later determined to be a better middle ground as to not use up all
60-
// of PGs default connection limit while simultaneously avoiding a lot
61-
// of connection churn.
62-
dbx.SetMaxIdleConns(3)
63-
6445
return &sqlQuerier{
6546
db: dbx,
6647
sdb: dbx,

0 commit comments

Comments
 (0)