Open F21 opened 3 years ago
/cc @bradfitz @kardianos
@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?
@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.
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.
It would be nice to have a way to ask
sql.DB
to shutdown its connections gracefully. There's aSetMaxIdleConns()
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 theConnector
interface via a function hook: https://github.com/Boostport/dynamic-database-configThe 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 forcesql.DB
to create new connections, which using our driver wrapper would produce connections with the new credentials.