1
0
mirror of https://github.com/golang/go synced 2024-11-18 23:05:06 -07:00

database/sql: use a value type instead of a pointer

Follow-up to https://golang.org/cl/107020044/
Also add a little comment.

LGTM=ruiu, josharian
R=josharian, ruiu
CC=golang-codereviews
https://golang.org/cl/139760043
This commit is contained in:
Brad Fitzpatrick 2014-08-28 11:07:29 -07:00
parent 5ea131f442
commit 558bd8e1d9

View File

@ -198,7 +198,7 @@ type DB struct {
mu sync.Mutex // protects following fields mu sync.Mutex // protects following fields
freeConn []*driverConn freeConn []*driverConn
connRequests []chan *connRequest connRequests []chan connRequest
numOpen int numOpen int
pendingOpens int pendingOpens int
// Used to signal the need for new connections // Used to signal the need for new connections
@ -626,14 +626,11 @@ func (db *DB) conn() (*driverConn, error) {
if db.maxOpen > 0 && db.numOpen >= db.maxOpen && len(db.freeConn) == 0 { if db.maxOpen > 0 && db.numOpen >= db.maxOpen && len(db.freeConn) == 0 {
// Make the connRequest channel. It's buffered so that the // Make the connRequest channel. It's buffered so that the
// connectionOpener doesn't block while waiting for the req to be read. // connectionOpener doesn't block while waiting for the req to be read.
req := make(chan *connRequest, 1) req := make(chan connRequest, 1)
db.connRequests = append(db.connRequests, req) db.connRequests = append(db.connRequests, req)
db.maybeOpenNewConnections() db.maybeOpenNewConnections()
db.mu.Unlock() db.mu.Unlock()
ret := <-req ret := <-req
if ret == nil {
return nil, errDBClosed
}
return ret.conn, ret.err return ret.conn, ret.err
} }
@ -786,12 +783,15 @@ func (db *DB) putConn(dc *driverConn, err error) {
func (db *DB) putConnDBLocked(dc *driverConn, err error) bool { func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
if c := len(db.connRequests); c > 0 { if c := len(db.connRequests); c > 0 {
req := db.connRequests[0] req := db.connRequests[0]
// This copy is O(n) but in practice faster than a linked list.
// TODO: consider compacting it down less often and
// moving the base instead?
copy(db.connRequests, db.connRequests[1:]) copy(db.connRequests, db.connRequests[1:])
db.connRequests = db.connRequests[:c-1] db.connRequests = db.connRequests[:c-1]
if err == nil { if err == nil {
dc.inUse = true dc.inUse = true
} }
req <- &connRequest{ req <- connRequest{
conn: dc, conn: dc,
err: err, err: err,
} }