2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2019-06-04 14:05:29 +03:00
|
|
|
"context"
|
2016-03-12 11:52:13 +03:00
|
|
|
"errors"
|
2016-03-12 13:41:02 +03:00
|
|
|
"net"
|
2016-03-17 19:00:47 +03:00
|
|
|
"sync"
|
2016-03-12 11:52:13 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9/internal"
|
2016-04-09 14:52:01 +03:00
|
|
|
)
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2020-07-16 09:52:07 +03:00
|
|
|
var (
|
2021-05-13 05:33:38 +03:00
|
|
|
// ErrClosed performs any operation on the closed client will return this error.
|
|
|
|
ErrClosed = errors.New("redis: client is closed")
|
|
|
|
|
2023-09-20 14:55:23 +03:00
|
|
|
// ErrPoolExhausted is returned from a pool connection method
|
|
|
|
// when the maximum number of database connections in the pool has been reached.
|
|
|
|
ErrPoolExhausted = errors.New("redis: connection pool exhausted")
|
|
|
|
|
2021-05-13 05:33:38 +03:00
|
|
|
// ErrPoolTimeout timed out waiting to get a connection from the connection pool.
|
2020-07-16 09:52:07 +03:00
|
|
|
ErrPoolTimeout = errors.New("redis: connection pool timeout")
|
|
|
|
)
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
var timers = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2017-03-04 02:45:40 +03:00
|
|
|
t := time.NewTimer(time.Hour)
|
|
|
|
t.Stop()
|
|
|
|
return t
|
2016-03-17 19:00:47 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
// Stats contains pool state information and accumulated stats.
|
|
|
|
type Stats struct {
|
2016-03-12 11:52:13 +03:00
|
|
|
Hits uint32 // number of times free connection was found in the pool
|
2017-09-22 12:23:46 +03:00
|
|
|
Misses uint32 // number of times free connection was NOT found in the pool
|
2016-03-12 11:52:13 +03:00
|
|
|
Timeouts uint32 // number of times a wait timeout occurred
|
|
|
|
|
2017-09-11 09:10:17 +03:00
|
|
|
TotalConns uint32 // number of total connections in the pool
|
2018-05-28 17:27:24 +03:00
|
|
|
IdleConns uint32 // number of idle connections in the pool
|
2017-09-11 09:10:17 +03:00
|
|
|
StaleConns uint32 // number of stale connections removed from the pool
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Pooler interface {
|
2019-06-14 16:00:03 +03:00
|
|
|
NewConn(context.Context) (*Conn, error)
|
2017-04-17 15:43:58 +03:00
|
|
|
CloseConn(*Conn) error
|
|
|
|
|
2019-06-04 14:05:29 +03:00
|
|
|
Get(context.Context) (*Conn, error)
|
2020-08-15 15:36:02 +03:00
|
|
|
Put(context.Context, *Conn)
|
|
|
|
Remove(context.Context, *Conn, error)
|
2017-04-17 15:43:58 +03:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
Len() int
|
2018-05-28 17:27:24 +03:00
|
|
|
IdleLen() int
|
2016-10-02 15:44:01 +03:00
|
|
|
Stats() *Stats
|
2017-04-17 15:43:58 +03:00
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
Close() error
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
type Options struct {
|
2023-02-12 13:50:25 +03:00
|
|
|
Dialer func(context.Context) (net.Conn, error)
|
2017-06-29 14:26:52 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolFIFO bool
|
|
|
|
PoolSize int
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
MinIdleConns int
|
|
|
|
MaxIdleConns int
|
2023-09-20 14:55:23 +03:00
|
|
|
MaxActiveConns int
|
2022-07-28 15:11:35 +03:00
|
|
|
ConnMaxIdleTime time.Duration
|
|
|
|
ConnMaxLifetime time.Duration
|
2024-07-03 16:21:56 +03:00
|
|
|
ConnChecker func(net.Conn) error
|
2017-06-29 14:26:52 +03:00
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2020-07-14 01:38:11 +03:00
|
|
|
type lastDialErrorWrap struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
type ConnPool struct {
|
2022-07-28 15:11:35 +03:00
|
|
|
cfg *Options
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-10-11 18:03:55 +03:00
|
|
|
dialErrorsNum uint32 // atomic
|
2020-07-14 01:38:11 +03:00
|
|
|
lastDialError atomic.Value
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
queue chan struct{}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
connsMu sync.Mutex
|
|
|
|
conns []*Conn
|
|
|
|
idleConns []*Conn
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
poolSize int
|
|
|
|
idleConnsLen int
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
stats Stats
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2023-02-12 13:50:25 +03:00
|
|
|
_closed uint32 // atomic
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
var _ Pooler = (*ConnPool)(nil)
|
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
func NewConnPool(opt *Options) *ConnPool {
|
2016-03-12 11:52:13 +03:00
|
|
|
p := &ConnPool{
|
2022-07-28 15:11:35 +03:00
|
|
|
cfg: opt,
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
queue: make(chan struct{}, opt.PoolSize),
|
|
|
|
conns: make([]*Conn, 0, opt.PoolSize),
|
2018-05-28 17:27:24 +03:00
|
|
|
idleConns: make([]*Conn, 0, opt.PoolSize),
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
2020-04-30 12:46:50 +03:00
|
|
|
|
2020-04-30 09:34:48 +03:00
|
|
|
p.connsMu.Lock()
|
2019-07-28 09:56:57 +03:00
|
|
|
p.checkMinIdleConns()
|
2020-04-30 09:34:48 +03:00
|
|
|
p.connsMu.Unlock()
|
2018-05-28 17:27:24 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
return p
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
func (p *ConnPool) checkMinIdleConns() {
|
2022-07-28 15:11:35 +03:00
|
|
|
if p.cfg.MinIdleConns == 0 {
|
2018-05-28 17:27:24 +03:00
|
|
|
return
|
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
for p.poolSize < p.cfg.PoolSize && p.idleConnsLen < p.cfg.MinIdleConns {
|
2023-02-14 13:01:53 +03:00
|
|
|
select {
|
|
|
|
case p.queue <- struct{}{}:
|
|
|
|
p.poolSize++
|
|
|
|
p.idleConnsLen++
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := p.addIdleConn()
|
|
|
|
if err != nil && err != ErrClosed {
|
|
|
|
p.connsMu.Lock()
|
|
|
|
p.poolSize--
|
|
|
|
p.idleConnsLen--
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
p.freeTurn()
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:53:40 +03:00
|
|
|
func (p *ConnPool) addIdleConn() error {
|
2019-08-07 16:12:01 +03:00
|
|
|
cn, err := p.dialConn(context.TODO(), true)
|
2018-05-28 17:27:24 +03:00
|
|
|
if err != nil {
|
2019-07-28 10:53:40 +03:00
|
|
|
return err
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
p.connsMu.Lock()
|
2021-08-19 11:56:23 +03:00
|
|
|
defer p.connsMu.Unlock()
|
|
|
|
|
|
|
|
// It is not allowed to add new connections to the closed connection pool.
|
|
|
|
if p.closed() {
|
|
|
|
_ = cn.Close()
|
|
|
|
return ErrClosed
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
p.conns = append(p.conns, cn)
|
|
|
|
p.idleConns = append(p.idleConns, cn)
|
2019-07-28 10:53:40 +03:00
|
|
|
return nil
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 16:00:03 +03:00
|
|
|
func (p *ConnPool) NewConn(ctx context.Context) (*Conn, error) {
|
2019-08-07 16:12:01 +03:00
|
|
|
return p.newConn(ctx, false)
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:12:01 +03:00
|
|
|
func (p *ConnPool) newConn(ctx context.Context, pooled bool) (*Conn, error) {
|
2023-09-20 14:55:23 +03:00
|
|
|
if p.closed() {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:14:50 +03:00
|
|
|
p.connsMu.Lock()
|
2023-09-20 14:55:23 +03:00
|
|
|
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
|
2024-02-14 23:14:50 +03:00
|
|
|
p.connsMu.Unlock()
|
2023-09-20 14:55:23 +03:00
|
|
|
return nil, ErrPoolExhausted
|
|
|
|
}
|
2024-02-14 23:14:50 +03:00
|
|
|
p.connsMu.Unlock()
|
2023-09-20 14:55:23 +03:00
|
|
|
|
2019-08-07 16:12:01 +03:00
|
|
|
cn, err := p.dialConn(ctx, pooled)
|
2018-05-28 17:27:24 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.connsMu.Lock()
|
2021-08-19 11:56:23 +03:00
|
|
|
defer p.connsMu.Unlock()
|
|
|
|
|
2024-02-14 23:14:50 +03:00
|
|
|
if p.cfg.MaxActiveConns > 0 && p.poolSize >= p.cfg.MaxActiveConns {
|
|
|
|
_ = cn.Close()
|
|
|
|
return nil, ErrPoolExhausted
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
p.conns = append(p.conns, cn)
|
2018-05-28 17:27:24 +03:00
|
|
|
if pooled {
|
2019-06-17 12:32:40 +03:00
|
|
|
// If pool is full remove the cn on next Put.
|
2022-07-28 15:11:35 +03:00
|
|
|
if p.poolSize >= p.cfg.PoolSize {
|
2018-05-28 17:27:24 +03:00
|
|
|
cn.pooled = false
|
2019-06-17 12:32:40 +03:00
|
|
|
} else {
|
|
|
|
p.poolSize++
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 10:56:09 +03:00
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
return cn, nil
|
|
|
|
}
|
|
|
|
|
2019-08-07 16:12:01 +03:00
|
|
|
func (p *ConnPool) dialConn(ctx context.Context, pooled bool) (*Conn, error) {
|
2017-04-17 15:43:58 +03:00
|
|
|
if p.closed() {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
if atomic.LoadUint32(&p.dialErrorsNum) >= uint32(p.cfg.PoolSize) {
|
2017-10-11 18:03:55 +03:00
|
|
|
return nil, p.getLastDialError()
|
2017-06-29 14:26:52 +03:00
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
netConn, err := p.cfg.Dialer(ctx)
|
2016-03-12 13:41:02 +03:00
|
|
|
if err != nil {
|
2017-06-29 14:26:52 +03:00
|
|
|
p.setLastDialError(err)
|
2022-07-28 15:11:35 +03:00
|
|
|
if atomic.AddUint32(&p.dialErrorsNum, 1) == uint32(p.cfg.PoolSize) {
|
2017-06-29 14:26:52 +03:00
|
|
|
go p.tryDial()
|
|
|
|
}
|
2016-03-12 13:41:02 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-17 15:43:58 +03:00
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
cn := NewConn(netConn)
|
|
|
|
cn.pooled = pooled
|
|
|
|
return cn, nil
|
2016-03-12 13:41:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
func (p *ConnPool) tryDial() {
|
|
|
|
for {
|
2017-08-04 01:11:35 +03:00
|
|
|
if p.closed() {
|
|
|
|
return
|
|
|
|
}
|
2017-08-04 14:43:50 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
conn, err := p.cfg.Dialer(context.Background())
|
2017-06-29 14:26:52 +03:00
|
|
|
if err != nil {
|
|
|
|
p.setLastDialError(err)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StoreUint32(&p.dialErrorsNum, 0)
|
|
|
|
_ = conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) setLastDialError(err error) {
|
2020-07-14 01:38:11 +03:00
|
|
|
p.lastDialError.Store(&lastDialErrorWrap{err: err})
|
2017-06-29 14:26:52 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:03:55 +03:00
|
|
|
func (p *ConnPool) getLastDialError() error {
|
2020-07-14 01:38:11 +03:00
|
|
|
err, _ := p.lastDialError.Load().(*lastDialErrorWrap)
|
|
|
|
if err != nil {
|
|
|
|
return err.err
|
|
|
|
}
|
|
|
|
return nil
|
2017-06-29 14:26:52 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Get returns existed connection from the pool or creates a new one.
|
2019-06-08 15:36:00 +03:00
|
|
|
func (p *ConnPool) Get(ctx context.Context) (*Conn, error) {
|
2017-02-08 12:24:09 +03:00
|
|
|
if p.closed() {
|
2018-05-28 17:27:24 +03:00
|
|
|
return nil, ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2021-04-08 09:44:31 +03:00
|
|
|
if err := p.waitTurn(ctx); err != nil {
|
2018-05-28 17:27:24 +03:00
|
|
|
return nil, err
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
for {
|
2018-05-28 17:27:24 +03:00
|
|
|
p.connsMu.Lock()
|
2021-08-19 11:56:23 +03:00
|
|
|
cn, err := p.popIdle()
|
2018-05-28 17:27:24 +03:00
|
|
|
p.connsMu.Unlock()
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2021-08-19 11:56:23 +03:00
|
|
|
if err != nil {
|
2023-10-30 18:42:26 +03:00
|
|
|
p.freeTurn()
|
2021-08-19 11:56:23 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
if cn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
if !p.isHealthyConn(cn) {
|
2018-08-12 10:08:21 +03:00
|
|
|
_ = p.CloseConn(cn)
|
2016-10-02 15:44:01 +03:00
|
|
|
continue
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-10-02 15:44:01 +03:00
|
|
|
|
|
|
|
atomic.AddUint32(&p.stats.Hits, 1)
|
2018-05-28 17:27:24 +03:00
|
|
|
return cn, nil
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2017-09-22 12:23:46 +03:00
|
|
|
atomic.AddUint32(&p.stats.Misses, 1)
|
|
|
|
|
2019-08-07 16:12:01 +03:00
|
|
|
newcn, err := p.newConn(ctx, true)
|
2016-03-17 19:00:47 +03:00
|
|
|
if err != nil {
|
2018-05-28 17:27:24 +03:00
|
|
|
p.freeTurn()
|
|
|
|
return nil, err
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
return newcn, nil
|
|
|
|
}
|
|
|
|
|
2019-06-08 15:36:00 +03:00
|
|
|
func (p *ConnPool) waitTurn(ctx context.Context) error {
|
2019-07-04 11:18:06 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2019-06-08 15:36:00 +03:00
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
select {
|
|
|
|
case p.queue <- struct{}{}:
|
|
|
|
return nil
|
|
|
|
default:
|
2019-07-04 11:18:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
timer := timers.Get().(*time.Timer)
|
2022-07-28 15:11:35 +03:00
|
|
|
timer.Reset(p.cfg.PoolTimeout)
|
2019-07-04 11:18:06 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
timers.Put(timer)
|
|
|
|
return ctx.Err()
|
|
|
|
case p.queue <- struct{}{}:
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
2019-07-04 11:18:06 +03:00
|
|
|
timers.Put(timer)
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
|
|
|
timers.Put(timer)
|
|
|
|
atomic.AddUint32(&p.stats.Timeouts, 1)
|
|
|
|
return ErrPoolTimeout
|
2018-05-28 17:27:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) freeTurn() {
|
|
|
|
<-p.queue
|
|
|
|
}
|
|
|
|
|
2021-08-19 11:56:23 +03:00
|
|
|
func (p *ConnPool) popIdle() (*Conn, error) {
|
|
|
|
if p.closed() {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
2021-07-16 06:58:01 +03:00
|
|
|
n := len(p.idleConns)
|
|
|
|
if n == 0 {
|
2021-08-19 11:56:23 +03:00
|
|
|
return nil, nil
|
2017-06-29 16:53:49 +03:00
|
|
|
}
|
|
|
|
|
2021-07-16 06:58:01 +03:00
|
|
|
var cn *Conn
|
2022-07-28 15:11:35 +03:00
|
|
|
if p.cfg.PoolFIFO {
|
2021-07-16 06:58:01 +03:00
|
|
|
cn = p.idleConns[0]
|
|
|
|
copy(p.idleConns, p.idleConns[1:])
|
|
|
|
p.idleConns = p.idleConns[:n-1]
|
|
|
|
} else {
|
|
|
|
idx := n - 1
|
|
|
|
cn = p.idleConns[idx]
|
|
|
|
p.idleConns = p.idleConns[:idx]
|
|
|
|
}
|
2018-05-28 17:27:24 +03:00
|
|
|
p.idleConnsLen--
|
|
|
|
p.checkMinIdleConns()
|
2021-08-19 11:56:23 +03:00
|
|
|
return cn, nil
|
2017-06-29 16:53:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:36:02 +03:00
|
|
|
func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
|
2019-08-09 15:11:52 +03:00
|
|
|
if cn.rd.Buffered() > 0 {
|
2020-08-15 15:36:02 +03:00
|
|
|
internal.Logger.Printf(ctx, "Conn has unread data")
|
|
|
|
p.Remove(ctx, cn, BadConnError{})
|
2019-08-09 15:11:52 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
if !cn.pooled {
|
2020-08-15 15:36:02 +03:00
|
|
|
p.Remove(ctx, cn, nil)
|
2018-05-28 17:27:24 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
var shouldCloseConn bool
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
p.connsMu.Lock()
|
2022-07-28 15:11:35 +03:00
|
|
|
|
|
|
|
if p.cfg.MaxIdleConns == 0 || p.idleConnsLen < p.cfg.MaxIdleConns {
|
|
|
|
p.idleConns = append(p.idleConns, cn)
|
|
|
|
p.idleConnsLen++
|
|
|
|
} else {
|
|
|
|
p.removeConn(cn)
|
|
|
|
shouldCloseConn = true
|
|
|
|
}
|
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
p.connsMu.Unlock()
|
2022-07-28 15:11:35 +03:00
|
|
|
|
2018-05-28 17:27:24 +03:00
|
|
|
p.freeTurn()
|
2022-07-28 15:11:35 +03:00
|
|
|
|
|
|
|
if shouldCloseConn {
|
|
|
|
_ = p.closeConn(cn)
|
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2023-02-12 13:50:25 +03:00
|
|
|
func (p *ConnPool) Remove(_ context.Context, cn *Conn, reason error) {
|
2019-06-17 12:32:40 +03:00
|
|
|
p.removeConnWithLock(cn)
|
2018-05-28 17:27:24 +03:00
|
|
|
p.freeTurn()
|
|
|
|
_ = p.closeConn(cn)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *ConnPool) CloseConn(cn *Conn) error {
|
2019-06-17 12:32:40 +03:00
|
|
|
p.removeConnWithLock(cn)
|
2018-05-28 17:27:24 +03:00
|
|
|
return p.closeConn(cn)
|
|
|
|
}
|
|
|
|
|
2019-06-17 12:32:40 +03:00
|
|
|
func (p *ConnPool) removeConnWithLock(cn *Conn) {
|
2016-09-10 16:54:49 +03:00
|
|
|
p.connsMu.Lock()
|
2022-07-28 15:11:35 +03:00
|
|
|
defer p.connsMu.Unlock()
|
2019-06-17 12:32:40 +03:00
|
|
|
p.removeConn(cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) removeConn(cn *Conn) {
|
2016-03-17 19:00:47 +03:00
|
|
|
for i, c := range p.conns {
|
|
|
|
if c == cn {
|
|
|
|
p.conns = append(p.conns[:i], p.conns[i+1:]...)
|
2018-05-28 17:27:24 +03:00
|
|
|
if cn.pooled {
|
|
|
|
p.poolSize--
|
|
|
|
p.checkMinIdleConns()
|
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
break
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 13:01:53 +03:00
|
|
|
atomic.AddUint32(&p.stats.StaleConns, 1)
|
2017-04-17 15:43:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) closeConn(cn *Conn) error {
|
|
|
|
return cn.Close()
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Len returns total number of connections.
|
|
|
|
func (p *ConnPool) Len() int {
|
2016-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Lock()
|
2018-08-12 10:08:21 +03:00
|
|
|
n := len(p.conns)
|
2016-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Unlock()
|
2018-05-28 17:27:24 +03:00
|
|
|
return n
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2018-08-12 10:08:21 +03:00
|
|
|
// IdleLen returns number of idle connections.
|
2018-05-28 17:27:24 +03:00
|
|
|
func (p *ConnPool) IdleLen() int {
|
2018-05-28 17:27:24 +03:00
|
|
|
p.connsMu.Lock()
|
|
|
|
n := p.idleConnsLen
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
return n
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
func (p *ConnPool) Stats() *Stats {
|
|
|
|
return &Stats{
|
2017-09-11 09:10:17 +03:00
|
|
|
Hits: atomic.LoadUint32(&p.stats.Hits),
|
2017-09-22 12:23:46 +03:00
|
|
|
Misses: atomic.LoadUint32(&p.stats.Misses),
|
2017-09-11 09:10:17 +03:00
|
|
|
Timeouts: atomic.LoadUint32(&p.stats.Timeouts),
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
TotalConns: uint32(p.Len()),
|
2022-11-18 16:32:17 +03:00
|
|
|
IdleConns: uint32(p.IdleLen()),
|
2017-09-11 09:10:17 +03:00
|
|
|
StaleConns: atomic.LoadUint32(&p.stats.StaleConns),
|
2016-10-02 15:44:01 +03:00
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-08 12:24:09 +03:00
|
|
|
func (p *ConnPool) closed() bool {
|
2017-06-29 14:26:52 +03:00
|
|
|
return atomic.LoadUint32(&p._closed) == 1
|
2016-03-16 17:57:24 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
func (p *ConnPool) Filter(fn func(*Conn) bool) error {
|
|
|
|
p.connsMu.Lock()
|
2020-09-05 10:56:09 +03:00
|
|
|
defer p.connsMu.Unlock()
|
|
|
|
|
|
|
|
var firstErr error
|
2017-06-29 16:53:49 +03:00
|
|
|
for _, cn := range p.conns {
|
|
|
|
if fn(cn) {
|
|
|
|
if err := p.closeConn(cn); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
func (p *ConnPool) Close() error {
|
2017-06-29 14:26:52 +03:00
|
|
|
if !atomic.CompareAndSwapUint32(&p._closed, 0, 1) {
|
2016-03-14 14:17:33 +03:00
|
|
|
return ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
var firstErr error
|
2017-06-29 16:53:49 +03:00
|
|
|
p.connsMu.Lock()
|
2016-03-17 19:00:47 +03:00
|
|
|
for _, cn := range p.conns {
|
2017-04-17 15:43:58 +03:00
|
|
|
if err := p.closeConn(cn); err != nil && firstErr == nil {
|
2016-12-03 18:30:13 +03:00
|
|
|
firstErr = err
|
2016-03-16 17:57:24 +03:00
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
p.conns = nil
|
2018-05-28 17:27:24 +03:00
|
|
|
p.poolSize = 0
|
2018-05-28 17:27:24 +03:00
|
|
|
p.idleConns = nil
|
2018-05-28 17:27:24 +03:00
|
|
|
p.idleConnsLen = 0
|
|
|
|
p.connsMu.Unlock()
|
2016-03-16 17:57:24 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
return firstErr
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
func (p *ConnPool) isHealthyConn(cn *Conn) bool {
|
|
|
|
now := time.Now()
|
2016-03-12 15:42:12 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
if p.cfg.ConnMaxLifetime > 0 && now.Sub(cn.createdAt) >= p.cfg.ConnMaxLifetime {
|
|
|
|
return false
|
2019-06-17 12:32:40 +03:00
|
|
|
}
|
2022-07-28 15:11:35 +03:00
|
|
|
if p.cfg.ConnMaxIdleTime > 0 && now.Sub(cn.UsedAt()) >= p.cfg.ConnMaxIdleTime {
|
|
|
|
return false
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2019-06-17 12:32:40 +03:00
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
if connCheck(cn.netConn) != nil {
|
|
|
|
return false
|
2018-08-12 10:08:21 +03:00
|
|
|
}
|
|
|
|
|
2024-07-03 16:21:56 +03:00
|
|
|
if p.cfg.ConnChecker != nil {
|
|
|
|
if err := p.cfg.ConnChecker(cn.netConn); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
cn.SetUsedAt(now)
|
|
|
|
return true
|
2018-08-12 10:08:21 +03:00
|
|
|
}
|