-
Notifications
You must be signed in to change notification settings - Fork 2.2k
multi: change sql db default max connections #10426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,12 +7,19 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // defaultMaxConns is the number of permitted active and idle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // defaultMaxConnsPostgres is the number of permitted active and idle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // connections. We want to limit this so it isn't unlimited. We use the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // same value for the number of idle connections as, this can speed up | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // queries given a new connection doesn't need to be established each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultMaxConns = 25 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultMaxConnsPostgres = 10 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // defaultMaxConnsSqlite is the number of permitted active and idle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // connections. We want to limit this to 2 because SQLite allows for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // concurrent reads however writers are limited to 1 so we select 2 here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // to allow for concurrent reads but keep the number of connections low | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // to avoid write contention. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultMaxConnsSqlite = 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid duplicating these default values in the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // connIdleLifetime is the amount of time a connection can be idle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connIdleLifetime = 5 * time.Minute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,7 +122,7 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) { | |
| err) | ||
| } | ||
|
|
||
| maxConns := defaultMaxConns | ||
| maxConns := defaultMaxConnsPostgres | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if cfg.MaxConnections > 0 { | ||
| maxConns = cfg.MaxConnections | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,8 +130,13 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) { | |
| err) | ||
| } | ||
|
|
||
| db.SetMaxOpenConns(defaultMaxConns) | ||
| db.SetMaxIdleConns(defaultMaxConns) | ||
| maxConns := defaultMaxConnsSqlite | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if cfg.MaxConnections > 0 { | ||
| maxConns = cfg.MaxConnections | ||
| } | ||
|
|
||
| db.SetMaxOpenConns(maxConns) | ||
| db.SetMaxIdleConns(maxConns) | ||
| db.SetConnMaxLifetime(connIdleLifetime) | ||
| queries := sqlc.New(db) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid duplication and ensure consistency, these default values should be sourced from the
sqldbpackage where they are also defined.