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: dynamic reduce the connection pool size #9453

dojiong opened this issue Dec 27, 2014 · 1 comment

database/sql: dynamic reduce the connection pool size #9453

dojiong opened this issue Dec 27, 2014 · 1 comment


Copy link

@dojiong dojiong commented Dec 27, 2014

DB.SetMaxOpenConns could reduce the connection pool size, but the unnecssary connections won't close until DB's free connection size >= DB's idle connections size.

That is to say, when the pool is busy (lots of requests), the unnecessary connections won't be closed.

detailed discussion on golang-nuts

my patch to src/database/sql/sql.go

*************** func (db *DB) putConn(dc *driverConn, er
*** 797,802 ****
--- 797,806 ----
  // If a connRequest was fulfilled or the *driverConn was placed in the
  // freeConn list, then true is returned, otherwise false is returned.
  func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
+   if db.numOpen > db.maxOpen {
+       return false
+   }
    if c := len(db.connRequests); c > 0 {
        req := db.connRequests[0]
        // This copy is O(n) but in practice faster than a linked list.
@mikioh mikioh changed the title database.sql: dynamic reduce the connection pool size database/sql: dynamic reduce the connection pool size Dec 27, 2014
Copy link

@bradfitz bradfitz commented Dec 29, 2014

To contribute a fix to Go,

-- See on how to use Gerrit & git-codereview (we don't take or comment on patches on the issue tracker or in pull requests)
-- Include a new test
-- Write a detailed and self-contained (without referencing a thread elsewhere) commit message explaining the problem and fix.

@bradfitz bradfitz closed this in cce127a Apr 7, 2015
@mikioh mikioh added this to the Go1.5 milestone Apr 7, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.