Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 43d1f72

Browse files
authored
chore: Implement database limits from v1 (coder#4669)
Under scale if there wasn't a PostgreSQL connection available, an error was occurring instead of blocking for a new connection. This fixes it!
1 parent 7c238f1 commit 43d1f72

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

coderd/database/db.go

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ 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+
4564
return &sqlQuerier{
4665
db: dbx,
4766
sdb: dbx,

0 commit comments

Comments
 (0)