-
Notifications
You must be signed in to change notification settings - Fork 5
fix: add db connection time limits & close where possible #60
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -579,6 +579,7 @@ func (st *NestsDBStore) Migrate(migratePath string) error { | |
| if err != nil { | ||
| return fmt.Errorf("failed to connect to the DB: %w", err) | ||
| } | ||
| defer db.Close() | ||
|
Collaborator
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. Good to clean this one up, also, tho it would just leak this 1 connection on startup. |
||
|
|
||
| dbDriver, err := migrate_mysql.WithInstance(db, migrateConfig) | ||
| if err != nil { | ||
|
|
@@ -691,6 +692,8 @@ func NewNestsDBStore(config DBConfig, logger *logrus.Logger) (*NestsDBStore, err | |
|
|
||
| db.SetMaxOpenConns(config.MaxPool) | ||
| db.SetMaxIdleConns(5) | ||
| db.SetConnMaxLifetime(5 * time.Minute) | ||
| db.SetConnMaxIdleTime(5 * time.Minute) | ||
|
Collaborator
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. There's not really any reason for the max lifetime one. The max idle time seems reasonable. You only save 5 connections tho. |
||
|
|
||
| return &NestsDBStore{ | ||
| logger: logger, | ||
|
|
||
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.
I believe this is the only fix that is needed. The others wouldn't have major effect, since there is a limit of 5 idle connections. But this one likely leaks a connection... everytime you reload. So I'd be willing to bet you are reloading often. I'm not sure why so often tho :). But this would also explain why I don't see the issue. I last restarted sometime in 2025 and pretty much never reload.
But I think this should be fixed differently, maybe. I'm not sure it makes sense to do a real test of the DB connection in config validation. This could probably just be an sqlx.Open, instead.
Sorry for the delay in seeing this.