mirror of
https://github.com/jackc/pgx.git
synced 2025-05-30 03:03:10 +00:00
Track and check number of in-progress acquires
This commit is contained in:
parent
7477020000
commit
ecf158b086
31
conn_pool.go
31
conn_pool.go
@ -18,6 +18,7 @@ type ConnPool struct {
|
|||||||
availableConnections []*Conn
|
availableConnections []*Conn
|
||||||
cond *sync.Cond
|
cond *sync.Cond
|
||||||
config ConnConfig // config used when establishing connection
|
config ConnConfig // config used when establishing connection
|
||||||
|
inProgressConnects int
|
||||||
maxConnections int
|
maxConnections int
|
||||||
resetCount int
|
resetCount int
|
||||||
afterConnect func(*Conn) error
|
afterConnect func(*Conn) error
|
||||||
@ -133,33 +134,17 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// No connections are available, but we can create more
|
// No connections are available, but we can create more
|
||||||
if len(p.allConnections) < p.maxConnections {
|
if len(p.allConnections)+p.inProgressConnects < p.maxConnections {
|
||||||
// Create a placeholder connection.
|
|
||||||
placeholderConn := &Conn{}
|
|
||||||
p.allConnections = append(p.allConnections, placeholderConn)
|
|
||||||
// Create a new connection.
|
// Create a new connection.
|
||||||
// Carefull here: createConnectionUnlocked() removes the current lock,
|
// Careful here: createConnectionUnlocked() removes the current lock,
|
||||||
// creates a connection and then locks it back.
|
// creates a connection and then locks it back.
|
||||||
c, err := p.createConnectionUnlocked()
|
if c, err := p.createConnectionUnlocked(); err == nil {
|
||||||
// Take the placeholder out of the list of connections.
|
|
||||||
p.removeFromAllConnections(placeholderConn)
|
|
||||||
// Make sure create connection did not fail
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// If resetCount was updated since we started our connection, or
|
|
||||||
// there is no room in the list of allConnections
|
|
||||||
// (invalidateAcquired may remove our placeholder), try to re-acquire
|
|
||||||
// the connection.
|
|
||||||
if len(p.allConnections) < p.maxConnections {
|
|
||||||
// Put the new connection to the list.
|
|
||||||
c.poolResetCount = p.resetCount
|
c.poolResetCount = p.resetCount
|
||||||
p.allConnections = append(p.allConnections, c)
|
p.allConnections = append(p.allConnections, c)
|
||||||
return c, nil
|
return c, nil
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
// There is no room for the just created connection.
|
|
||||||
// Close it and try to re-acquire.
|
|
||||||
c.Close()
|
|
||||||
} else {
|
} else {
|
||||||
// All connections are in use and we cannot create more
|
// All connections are in use and we cannot create more
|
||||||
if p.logLevel >= LogLevelWarn {
|
if p.logLevel >= LogLevelWarn {
|
||||||
@ -167,7 +152,7 @@ func (p *ConnPool) acquire(deadline *time.Time) (*Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wait until there is an available connection OR room to create a new connection
|
// Wait until there is an available connection OR room to create a new connection
|
||||||
for len(p.availableConnections) == 0 && len(p.allConnections) == p.maxConnections {
|
for len(p.availableConnections) == 0 && len(p.allConnections)+p.inProgressConnects == p.maxConnections {
|
||||||
if p.deadlinePassed(deadline) {
|
if p.deadlinePassed(deadline) {
|
||||||
return nil, errors.New("Timeout: All connections in pool are busy")
|
return nil, errors.New("Timeout: All connections in pool are busy")
|
||||||
}
|
}
|
||||||
@ -307,9 +292,11 @@ func (p *ConnPool) createConnection() (*Conn, error) {
|
|||||||
// To avoid this we put Connect(p.config) outside of the lock (it is thread safe)
|
// To avoid this we put Connect(p.config) outside of the lock (it is thread safe)
|
||||||
// what would allow us to make all the 20 connection in parallel (more or less).
|
// what would allow us to make all the 20 connection in parallel (more or less).
|
||||||
func (p *ConnPool) createConnectionUnlocked() (*Conn, error) {
|
func (p *ConnPool) createConnectionUnlocked() (*Conn, error) {
|
||||||
|
p.inProgressConnects++
|
||||||
p.cond.L.Unlock()
|
p.cond.L.Unlock()
|
||||||
c, err := Connect(p.config)
|
c, err := Connect(p.config)
|
||||||
p.cond.L.Lock()
|
p.cond.L.Lock()
|
||||||
|
p.inProgressConnects--
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user