mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Use sync.Once to guard pool.Close
This avoids the small possibility of 2 concurrent Close calls still double closing the channel.
This commit is contained in:
parent
fe366b2cf3
commit
80147fd7cc
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgconn"
|
"github.com/jackc/pgconn"
|
||||||
@ -78,7 +79,9 @@ type Pool struct {
|
|||||||
maxConnLifetime time.Duration
|
maxConnLifetime time.Duration
|
||||||
maxConnIdleTime time.Duration
|
maxConnIdleTime time.Duration
|
||||||
healthCheckPeriod time.Duration
|
healthCheckPeriod time.Duration
|
||||||
closeChan chan struct{}
|
|
||||||
|
closeOnce sync.Once
|
||||||
|
closeChan chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config is the configuration struct for creating a pool. It must be created by ParseConfig and then it can be
|
// Config is the configuration struct for creating a pool. It must be created by ParseConfig and then it can be
|
||||||
@ -326,14 +329,10 @@ func ParseConfig(connString string) (*Config, error) {
|
|||||||
// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned
|
// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned
|
||||||
// to pool and closed.
|
// to pool and closed.
|
||||||
func (p *Pool) Close() {
|
func (p *Pool) Close() {
|
||||||
// Check to see if the closeChan is closed before attempting to close it again, which can result in a panic.
|
p.closeOnce.Do(func() {
|
||||||
select {
|
|
||||||
case <-p.closeChan:
|
|
||||||
// NOOP because the channel is already closed.
|
|
||||||
default:
|
|
||||||
close(p.closeChan)
|
close(p.closeChan)
|
||||||
p.p.Close()
|
p.p.Close()
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) backgroundHealthCheck() {
|
func (p *Pool) backgroundHealthCheck() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user