2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal"
|
2016-04-09 14:52:01 +03:00
|
|
|
)
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
var ErrClosed = errors.New("redis: client is closed")
|
|
|
|
var 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
|
|
|
Requests uint32 // number of times a connection was requested by the pool
|
|
|
|
Hits uint32 // number of times free connection was found in the pool
|
|
|
|
Timeouts uint32 // number of times a wait timeout occurred
|
|
|
|
|
|
|
|
TotalConns uint32 // the number of total connections in the pool
|
|
|
|
FreeConns uint32 // the number of free connections in the pool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pooler interface {
|
2017-04-17 15:43:58 +03:00
|
|
|
NewConn() (*Conn, error)
|
|
|
|
CloseConn(*Conn) error
|
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
Get() (*Conn, bool, error)
|
2016-03-12 11:52:13 +03:00
|
|
|
Put(*Conn) error
|
2017-04-17 15:43:58 +03:00
|
|
|
Remove(*Conn) error
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
Len() int
|
|
|
|
FreeLen() 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 {
|
|
|
|
Dialer func() (net.Conn, error)
|
|
|
|
OnClose func(*Conn) error
|
|
|
|
|
|
|
|
PoolSize int
|
|
|
|
PoolTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
IdleCheckFrequency time.Duration
|
|
|
|
}
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
type ConnPool struct {
|
2017-06-29 14:26:52 +03:00
|
|
|
opt *Options
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
dialErrorsNum uint32 // atomic
|
|
|
|
_lastDialError atomic.Value
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
queue chan struct{}
|
|
|
|
|
|
|
|
connsMu sync.Mutex
|
|
|
|
conns []*Conn
|
|
|
|
|
|
|
|
freeConnsMu sync.Mutex
|
|
|
|
freeConns []*Conn
|
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
|
|
|
|
2017-06-29 14:26:52 +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{
|
2017-06-29 14:26:52 +03:00
|
|
|
opt: 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),
|
|
|
|
freeConns: make([]*Conn, 0, opt.PoolSize),
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
2017-06-29 14:26:52 +03:00
|
|
|
if opt.IdleTimeout > 0 && opt.IdleCheckFrequency > 0 {
|
|
|
|
go p.reaper(opt.IdleCheckFrequency)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
return p
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-15 15:04:35 +03:00
|
|
|
func (p *ConnPool) NewConn() (*Conn, error) {
|
2017-04-17 15:43:58 +03:00
|
|
|
if p.closed() {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
if atomic.LoadUint32(&p.dialErrorsNum) >= uint32(p.opt.PoolSize) {
|
|
|
|
return nil, p.lastDialError()
|
|
|
|
}
|
|
|
|
|
|
|
|
netConn, err := p.opt.Dialer()
|
2016-03-12 13:41:02 +03:00
|
|
|
if err != nil {
|
2017-06-29 14:26:52 +03:00
|
|
|
p.setLastDialError(err)
|
|
|
|
if atomic.AddUint32(&p.dialErrorsNum, 1) == uint32(p.opt.PoolSize) {
|
|
|
|
go p.tryDial()
|
|
|
|
}
|
2016-03-12 13:41:02 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-17 15:43:58 +03:00
|
|
|
|
|
|
|
cn := NewConn(netConn)
|
|
|
|
p.connsMu.Lock()
|
|
|
|
p.conns = append(p.conns, cn)
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
conn, err := p.opt.Dialer()
|
|
|
|
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) {
|
|
|
|
p._lastDialError.Store(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) lastDialError() error {
|
|
|
|
return p._lastDialError.Load().(error)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Get returns existed connection from the pool or creates a new one.
|
2016-09-29 15:07:04 +03:00
|
|
|
func (p *ConnPool) Get() (*Conn, bool, error) {
|
2017-02-08 12:24:09 +03:00
|
|
|
if p.closed() {
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint32(&p.stats.Requests, 1)
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
select {
|
2016-09-12 09:15:59 +03:00
|
|
|
case p.queue <- struct{}{}:
|
2017-06-29 12:54:49 +03:00
|
|
|
default:
|
|
|
|
timer := timers.Get().(*time.Timer)
|
2017-06-29 14:26:52 +03:00
|
|
|
timer.Reset(p.opt.PoolTimeout)
|
2017-06-29 12:54:49 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case p.queue <- struct{}{}:
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
timers.Put(timer)
|
|
|
|
case <-timer.C:
|
|
|
|
timers.Put(timer)
|
|
|
|
atomic.AddUint32(&p.stats.Timeouts, 1)
|
|
|
|
return nil, false, ErrPoolTimeout
|
2017-03-04 02:45:40 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
for {
|
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
cn := p.popFree()
|
|
|
|
p.freeConnsMu.Unlock()
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
if cn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-06-29 14:26:52 +03:00
|
|
|
if cn.IsStale(p.opt.IdleTimeout) {
|
2017-04-17 15:43:58 +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)
|
|
|
|
return cn, false, nil
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newcn, err := p.NewConn()
|
|
|
|
if err != nil {
|
2016-09-12 09:15:59 +03:00
|
|
|
<-p.queue
|
2016-09-29 15:07:04 +03:00
|
|
|
return nil, false, err
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 15:07:04 +03:00
|
|
|
return newcn, true, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2017-06-29 16:53:49 +03:00
|
|
|
func (p *ConnPool) popFree() *Conn {
|
|
|
|
if len(p.freeConns) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := len(p.freeConns) - 1
|
|
|
|
cn := p.freeConns[idx]
|
|
|
|
p.freeConns = p.freeConns[:idx]
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (p *ConnPool) Put(cn *Conn) error {
|
2016-07-02 15:52:10 +03:00
|
|
|
if data := cn.Rd.PeekBuffered(); data != nil {
|
2017-04-17 15:43:58 +03:00
|
|
|
internal.Logf("connection has unread data: %q", data)
|
|
|
|
return p.Remove(cn)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
p.freeConns = append(p.freeConns, cn)
|
|
|
|
p.freeConnsMu.Unlock()
|
2016-09-12 09:15:59 +03:00
|
|
|
<-p.queue
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *ConnPool) Remove(cn *Conn) error {
|
|
|
|
_ = p.CloseConn(cn)
|
2016-09-12 09:15:59 +03:00
|
|
|
<-p.queue
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
func (p *ConnPool) CloseConn(cn *Conn) error {
|
2016-09-10 16:54:49 +03:00
|
|
|
p.connsMu.Lock()
|
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:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-10-02 15:44:01 +03:00
|
|
|
p.connsMu.Unlock()
|
2017-04-17 15:43:58 +03:00
|
|
|
|
|
|
|
return p.closeConn(cn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) closeConn(cn *Conn) error {
|
2017-06-29 14:26:52 +03:00
|
|
|
if p.opt.OnClose != nil {
|
|
|
|
_ = p.opt.OnClose(cn)
|
2017-04-17 15:43:58 +03:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
l := len(p.conns)
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
return l
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FreeLen returns number of free connections.
|
|
|
|
func (p *ConnPool) FreeLen() int {
|
2016-03-17 19:00:47 +03:00
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
l := len(p.freeConns)
|
|
|
|
p.freeConnsMu.Unlock()
|
|
|
|
return l
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-10-02 15:44:01 +03:00
|
|
|
func (p *ConnPool) Stats() *Stats {
|
|
|
|
return &Stats{
|
|
|
|
Requests: atomic.LoadUint32(&p.stats.Requests),
|
|
|
|
Hits: atomic.LoadUint32(&p.stats.Hits),
|
|
|
|
Timeouts: atomic.LoadUint32(&p.stats.Timeouts),
|
|
|
|
TotalConns: uint32(p.Len()),
|
|
|
|
FreeConns: uint32(p.FreeLen()),
|
|
|
|
}
|
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 {
|
|
|
|
var firstErr error
|
|
|
|
p.connsMu.Lock()
|
|
|
|
for _, cn := range p.conns {
|
|
|
|
if fn(cn) {
|
|
|
|
if err := p.closeConn(cn); err != nil && firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
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
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
|
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
p.freeConns = nil
|
|
|
|
p.freeConnsMu.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
|
|
|
}
|
|
|
|
|
2016-09-10 16:54:49 +03:00
|
|
|
func (p *ConnPool) reapStaleConn() bool {
|
2016-03-17 19:00:47 +03:00
|
|
|
if len(p.freeConns) == 0 {
|
2016-09-10 16:54:49 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cn := p.freeConns[0]
|
2017-06-29 14:26:52 +03:00
|
|
|
if !cn.IsStale(p.opt.IdleTimeout) {
|
2016-09-10 16:54:49 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-17 15:43:58 +03:00
|
|
|
p.CloseConn(cn)
|
2016-09-10 16:54:49 +03:00
|
|
|
p.freeConns = append(p.freeConns[:0], p.freeConns[1:]...)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) ReapStaleConns() (int, error) {
|
|
|
|
var n int
|
|
|
|
for {
|
2016-09-12 09:15:59 +03:00
|
|
|
p.queue <- struct{}{}
|
2016-09-10 16:54:49 +03:00
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
|
|
|
|
reaped := p.reapStaleConn()
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
p.freeConnsMu.Unlock()
|
2016-09-12 09:15:59 +03:00
|
|
|
<-p.queue
|
2016-03-17 19:00:47 +03:00
|
|
|
|
2016-09-10 16:54:49 +03:00
|
|
|
if reaped {
|
|
|
|
n++
|
|
|
|
} else {
|
2016-03-12 15:42:12 +03:00
|
|
|
break
|
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
}
|
2016-09-10 16:54:49 +03:00
|
|
|
return n, nil
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func (p *ConnPool) reaper(frequency time.Duration) {
|
|
|
|
ticker := time.NewTicker(frequency)
|
2016-03-12 11:52:13 +03:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2017-04-02 17:10:47 +03:00
|
|
|
for range ticker.C {
|
2017-02-08 12:24:09 +03:00
|
|
|
if p.closed() {
|
2016-03-12 11:52:13 +03:00
|
|
|
break
|
|
|
|
}
|
2016-03-12 15:42:12 +03:00
|
|
|
n, err := p.ReapStaleConns()
|
|
|
|
if err != nil {
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf("ReapStaleConns failed: %s", err)
|
2016-03-17 19:00:47 +03:00
|
|
|
continue
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
s := p.Stats()
|
2016-04-09 14:52:01 +03:00
|
|
|
internal.Logf(
|
2016-03-17 19:00:47 +03:00
|
|
|
"reaper: removed %d stale conns (TotalConns=%d FreeConns=%d Requests=%d Hits=%d Timeouts=%d)",
|
|
|
|
n, s.TotalConns, s.FreeConns, s.Requests, s.Hits, s.Timeouts,
|
|
|
|
)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|