File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,25 @@ type DBTX interface {
4242// New creates a new database store using a SQL database connection.
4343func 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 ,
You can’t perform that action at this time.
0 commit comments