database: convert DB.{waitDuration,numClosed} to atomic type

Change-Id: I4a67a5e51a98938c07b3c38442f8d852db97cb2a
Reviewed-on: https://go-review.googlesource.com/c/go/+/426085
Run-TryBot: Jenny Rakoczy <jenny@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jenny Rakoczy <jenny@golang.org>
Run-TryBot: xie cui <523516579@qq.com>
Auto-Submit: Jenny Rakoczy <jenny@golang.org>
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
This commit is contained in:
cuiweixie 2022-08-27 11:34:08 +08:00 committed by Gopher Robot
parent 7fda98a8d9
commit e50dd0e974

View file

@ -453,15 +453,14 @@ var ErrNoRows = errors.New("sql: no rows in result set")
// connection is returned to DB's idle connection pool. The pool size
// can be controlled with SetMaxIdleConns.
type DB struct {
// Atomic access only. At top of struct to prevent mis-alignment
// on 32-bit platforms. Of type time.Duration.
waitDuration int64 // Total time waited for new connections.
// Total time waited for new connections.
waitDuration atomic.Int64
connector driver.Connector
// numClosed is an atomic counter which represents a total number of
// closed connections. Stmt.openStmt checks it before cleaning closed
// connections in Stmt.css.
numClosed uint64
numClosed atomic.Uint64
mu sync.Mutex // protects following fields
freeConn []*driverConn // free connections ordered by returnedAt oldest to newest
@ -651,7 +650,7 @@ func (dc *driverConn) finalClose() error {
dc.db.maybeOpenNewConnections()
dc.db.mu.Unlock()
atomic.AddUint64(&dc.db.numClosed, 1)
dc.db.numClosed.Add(1)
return err
}
@ -1171,7 +1170,7 @@ type DBStats struct {
// Stats returns database statistics.
func (db *DB) Stats() DBStats {
wait := atomic.LoadInt64(&db.waitDuration)
wait := db.waitDuration.Load()
db.mu.Lock()
defer db.mu.Unlock()
@ -1341,7 +1340,7 @@ func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn
delete(db.connRequests, reqKey)
db.mu.Unlock()
atomic.AddInt64(&db.waitDuration, int64(time.Since(waitStart)))
db.waitDuration.Add(int64(time.Since(waitStart)))
select {
default:
@ -1352,7 +1351,7 @@ func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn
}
return nil, ctx.Err()
case ret, ok := <-req:
atomic.AddInt64(&db.waitDuration, int64(time.Since(waitStart)))
db.waitDuration.Add(int64(time.Since(waitStart)))
if !ok {
return nil, errDBClosed
@ -1619,7 +1618,7 @@ func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error)
// the DB.
if cg == nil {
stmt.css = []connStmt{{dc, ds}}
stmt.lastNumClosed = atomic.LoadUint64(&db.numClosed)
stmt.lastNumClosed = db.numClosed.Load()
db.addDep(stmt, stmt)
}
return stmt, nil
@ -2649,7 +2648,7 @@ func (s *Stmt) removeClosedStmtLocked() {
if t > 10 {
t = 10
}
dbClosed := atomic.LoadUint64(&s.db.numClosed)
dbClosed := s.db.numClosed.Load()
if dbClosed-s.lastNumClosed < uint64(t) {
return
}