Skip to content
Permalink
Browse files

database/sql: correctly report MaxIdleClosed stat

Previously the MaxIdleClosed counter was incremented when added
to the free connection list, rather then when it wasn't added
to the free connection list. Flip this logic to correct.

Fixes golang#27792

Change-Id: I405302c14fb985369dab48fbe845e5651afc4ccf
Reviewed-on: https://go-review.googlesource.com/c/138578
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information...
kardianos committed Sep 30, 2018
1 parent 9aed4cc commit 7db509e682891f3bc501c7b23e32e02c64893557
Showing with 58 additions and 4 deletions.
  1. +6 −4 src/database/sql/sql.go
  2. +52 −0 src/database/sql/sql_test.go
@@ -1322,11 +1322,13 @@ func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
err: err,
}
return true
} else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
db.freeConn = append(db.freeConn, dc)
} else if err == nil && !db.closed {
if db.maxIdleConnsLocked() > len(db.freeConn) {
db.freeConn = append(db.freeConn, dc)
db.startCleanerLocked()
return true
}
db.maxIdleClosed++
db.startCleanerLocked()
return true
}
return false
}
@@ -3415,6 +3415,58 @@ func TestConnectionLeak(t *testing.T) {
wg.Wait()
}

func TestStatsMaxIdleClosedZero(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)

db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
db.SetConnMaxLifetime(0)

preMaxIdleClosed := db.Stats().MaxIdleClosed

for i := 0; i < 10; i++ {
rows, err := db.Query("SELECT|people|name|")
if err != nil {
t.Fatal(err)
}
rows.Close()
}

st := db.Stats()
maxIdleClosed := st.MaxIdleClosed - preMaxIdleClosed
t.Logf("MaxIdleClosed: %d", maxIdleClosed)
if maxIdleClosed != 0 {
t.Fatal("expected 0 max idle closed conns, got: ", maxIdleClosed)
}
}

func TestStatsMaxIdleClosedTen(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)

db.SetMaxOpenConns(1)
db.SetMaxIdleConns(0)
db.SetConnMaxLifetime(0)

preMaxIdleClosed := db.Stats().MaxIdleClosed

for i := 0; i < 10; i++ {
rows, err := db.Query("SELECT|people|name|")
if err != nil {
t.Fatal(err)
}
rows.Close()
}

st := db.Stats()
maxIdleClosed := st.MaxIdleClosed - preMaxIdleClosed
t.Logf("MaxIdleClosed: %d", maxIdleClosed)
if maxIdleClosed != 10 {
t.Fatal("expected 0 max idle closed conns, got: ", maxIdleClosed)
}
}

type nvcDriver struct {
fakeDriver
skipNamedValueCheck bool

0 comments on commit 7db509e

Please sign in to comment.
You can’t perform that action at this time.