Skip to content
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

database/sql: shutdown connections gracefully #42963

Open
F21 opened this issue Dec 3, 2020 · 4 comments
Open

database/sql: shutdown connections gracefully #42963

F21 opened this issue Dec 3, 2020 · 4 comments

Comments

@F21
Copy link

@F21 F21 commented Dec 3, 2020

It would be nice to have a way to ask sql.DB to shutdown its connections gracefully. There's a SetMaxIdleConns() method that can be set to -1 to clear out all idle connections. However, this does not shutdown any active connections gracefully.

Our use-case is due to database credentials being rotated using Vault or AWS's IAM credentials for RDS. In both instances, we are handed dynamically generated usernames and passwords that will be revoked after a certain amount of time. New credentials are then generated.

In order to update the credentials in a given sql.DB instance, we have written a simple wrapper driver that provides the ability to customize the creation of a new connection using the Connector interface via a function hook: https://github.com/Boostport/dynamic-database-config

The only thing that's missing is the ability to signal sql.DB to shutdown all connections gracefully, that is to allow all current queries to complete, but not allow new ones to be scheduled on the current connections and then closing them once the current queries connect. This would then free up connections and force sql.DB to create new connections, which using our driver wrapper would produce connections with the new credentials.

@cagedmantis cagedmantis added this to the Backlog milestone Dec 8, 2020
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Dec 8, 2020

@kardianos
Copy link
Contributor

@kardianos kardianos commented Dec 8, 2020

@F21 There is an outstanding CL to be merged next window https://go-review.googlesource.com/c/go/+/258360 that will call "Close" on a Connector.

Would that work for your situation?

@F21
Copy link
Author

@F21 F21 commented Dec 8, 2020

@kardianos I think CL258360 requires a call to db.Close() which closes the db for good. For my use-case, I'd like to close all the existing connections in a db gracefully without closing the db itself. Since there are no active connections, db can instantiate new connections with the correct credentials using the wrapper driver we published.

@kardianos
Copy link
Contributor

@kardianos kardianos commented Dec 9, 2020

I see. You want to change the connector connection string, then when an old connection comes in to the pool (or is already in the pool), close it.

I agree that this is an important concept a database interface should have. v2 should support this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants