2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
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"
|
|
|
|
|
|
|
|
"gopkg.in/bsm/ratelimit.v1"
|
|
|
|
|
2016-04-09 13:54:44 +03:00
|
|
|
"gopkg.in/redis.v3/internal"
|
|
|
|
)
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
var (
|
2016-03-14 14:17:33 +03:00
|
|
|
ErrClosed = errors.New("redis: client is closed")
|
2016-03-12 11:52:13 +03:00
|
|
|
ErrPoolTimeout = errors.New("redis: connection pool timeout")
|
2016-04-06 13:13:03 +03:00
|
|
|
errConnStale = errors.New("connection is stale")
|
2016-03-12 11:52:13 +03:00
|
|
|
)
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
var timers = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return time.NewTimer(0)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// PoolStats contains pool state information and accumulated stats.
|
2016-03-17 19:00:47 +03:00
|
|
|
// TODO: remove Waits
|
2016-03-12 11:52:13 +03:00
|
|
|
type PoolStats struct {
|
|
|
|
Requests uint32 // number of times a connection was requested by the pool
|
|
|
|
Hits uint32 // number of times free connection was found in the pool
|
|
|
|
Waits uint32 // number of times the pool had to wait for a connection
|
|
|
|
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 {
|
2016-03-15 15:04:35 +03:00
|
|
|
Get() (*Conn, error)
|
2016-03-12 11:52:13 +03:00
|
|
|
Put(*Conn) error
|
2016-03-17 19:00:47 +03:00
|
|
|
Remove(*Conn, error) error
|
2016-03-12 11:52:13 +03:00
|
|
|
Len() int
|
|
|
|
FreeLen() int
|
|
|
|
Stats() *PoolStats
|
2016-03-14 14:17:33 +03:00
|
|
|
Close() error
|
|
|
|
Closed() bool
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
type dialer func() (net.Conn, error)
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
type ConnPool struct {
|
2016-03-12 13:38:52 +03:00
|
|
|
_dial dialer
|
|
|
|
DialLimiter *ratelimit.RateLimiter
|
2016-03-16 17:57:24 +03:00
|
|
|
OnClose func(*Conn) error
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
poolTimeout time.Duration
|
|
|
|
idleTimeout time.Duration
|
|
|
|
|
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-03-17 19:00:47 +03:00
|
|
|
stats PoolStats
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
_closed int32 // atomic
|
2016-03-12 11:52:13 +03:00
|
|
|
lastErr atomic.Value
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
var _ Pooler = (*ConnPool)(nil)
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout, idleCheckFrequency time.Duration) *ConnPool {
|
2016-03-12 11:52:13 +03:00
|
|
|
p := &ConnPool{
|
2016-03-12 13:38:52 +03:00
|
|
|
_dial: dial,
|
|
|
|
DialLimiter: ratelimit.New(3*poolSize, time.Second),
|
2016-03-12 11:52:13 +03:00
|
|
|
|
|
|
|
poolTimeout: poolTimeout,
|
|
|
|
idleTimeout: idleTimeout,
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
queue: make(chan struct{}, poolSize),
|
|
|
|
conns: make([]*Conn, 0, poolSize),
|
|
|
|
freeConns: make([]*Conn, 0, poolSize),
|
2016-03-12 15:42:12 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
for i := 0; i < poolSize; i++ {
|
|
|
|
p.queue <- struct{}{}
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
if idleTimeout > 0 && idleCheckFrequency > 0 {
|
|
|
|
go p.reaper(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-12 13:41:02 +03:00
|
|
|
func (p *ConnPool) dial() (net.Conn, error) {
|
2016-03-12 13:38:52 +03:00
|
|
|
if p.DialLimiter != nil && p.DialLimiter.Limit() {
|
2016-03-12 11:52:13 +03:00
|
|
|
err := fmt.Errorf(
|
|
|
|
"redis: you open connections too fast (last_error=%q)",
|
|
|
|
p.loadLastErr(),
|
|
|
|
)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
cn, err := p._dial()
|
2016-03-12 11:52:13 +03:00
|
|
|
if err != nil {
|
|
|
|
p.storeLastErr(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cn, nil
|
|
|
|
}
|
|
|
|
|
2016-03-15 15:04:35 +03:00
|
|
|
func (p *ConnPool) NewConn() (*Conn, error) {
|
2016-03-12 13:41:02 +03:00
|
|
|
netConn, err := p.dial()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewConn(netConn), nil
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
func (p *ConnPool) PopFree() *Conn {
|
|
|
|
timer := timers.Get().(*time.Timer)
|
|
|
|
if !timer.Reset(p.poolTimeout) {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-p.queue:
|
|
|
|
timers.Put(timer)
|
|
|
|
case <-timer.C:
|
|
|
|
timers.Put(timer)
|
|
|
|
atomic.AddUint32(&p.stats.Timeouts, 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
cn := p.popFree()
|
|
|
|
p.freeConnsMu.Unlock()
|
|
|
|
|
|
|
|
if cn == nil {
|
|
|
|
p.queue <- struct{}{}
|
|
|
|
}
|
|
|
|
return cn
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
// Get returns existed connection from the pool or creates a new one.
|
2016-03-15 15:04:35 +03:00
|
|
|
func (p *ConnPool) Get() (*Conn, error) {
|
2016-03-14 14:17:33 +03:00
|
|
|
if p.Closed() {
|
2016-03-15 15:04:35 +03:00
|
|
|
return nil, ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint32(&p.stats.Requests, 1)
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
timer := timers.Get().(*time.Timer)
|
|
|
|
if !timer.Reset(p.poolTimeout) {
|
|
|
|
<-timer.C
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
select {
|
|
|
|
case <-p.queue:
|
|
|
|
timers.Put(timer)
|
|
|
|
case <-timer.C:
|
|
|
|
timers.Put(timer)
|
|
|
|
atomic.AddUint32(&p.stats.Timeouts, 1)
|
|
|
|
return nil, ErrPoolTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
cn := p.popFree()
|
|
|
|
p.freeConnsMu.Unlock()
|
|
|
|
|
|
|
|
if cn != nil {
|
|
|
|
atomic.AddUint32(&p.stats.Hits, 1)
|
|
|
|
if !cn.IsStale(p.idleTimeout) {
|
|
|
|
return cn, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
_ = cn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
newcn, err := p.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
p.queue <- struct{}{}
|
|
|
|
return nil, err
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Lock()
|
|
|
|
if cn != nil {
|
|
|
|
p.remove(cn, errConnStale)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
p.conns = append(p.conns, newcn)
|
|
|
|
p.connsMu.Unlock()
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
return newcn, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) Put(cn *Conn) error {
|
|
|
|
if cn.Rd.Buffered() != 0 {
|
|
|
|
b, _ := cn.Rd.Peek(cn.Rd.Buffered())
|
|
|
|
err := fmt.Errorf("connection has unread data: %q", b)
|
2016-04-09 13:54:44 +03:00
|
|
|
internal.Logf(err.Error())
|
2016-03-17 19:00:47 +03:00
|
|
|
return p.Remove(cn, err)
|
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()
|
|
|
|
p.queue <- struct{}{}
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
func (p *ConnPool) Remove(cn *Conn, reason error) error {
|
2016-03-15 15:04:35 +03:00
|
|
|
_ = cn.Close()
|
2016-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Lock()
|
|
|
|
p.remove(cn, reason)
|
|
|
|
p.connsMu.Unlock()
|
|
|
|
p.queue <- struct{}{}
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:00:47 +03:00
|
|
|
func (p *ConnPool) remove(cn *Conn, reason error) {
|
2016-03-12 15:42:12 +03:00
|
|
|
p.storeLastErr(reason.Error())
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) Stats() *PoolStats {
|
2016-03-17 19:00:47 +03:00
|
|
|
stats := PoolStats{}
|
2016-03-12 11:52:13 +03:00
|
|
|
stats.Requests = atomic.LoadUint32(&p.stats.Requests)
|
2016-03-17 19:00:47 +03:00
|
|
|
stats.Hits = atomic.LoadUint32(&p.stats.Hits)
|
2016-03-12 11:52:13 +03:00
|
|
|
stats.Waits = atomic.LoadUint32(&p.stats.Waits)
|
|
|
|
stats.Timeouts = atomic.LoadUint32(&p.stats.Timeouts)
|
|
|
|
stats.TotalConns = uint32(p.Len())
|
|
|
|
stats.FreeConns = uint32(p.FreeLen())
|
|
|
|
return &stats
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func (p *ConnPool) Closed() bool {
|
|
|
|
return atomic.LoadInt32(&p._closed) == 1
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (p *ConnPool) Close() (retErr error) {
|
|
|
|
if !atomic.CompareAndSwapInt32(&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-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Lock()
|
2016-03-16 17:57:24 +03:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Close all connections.
|
2016-03-17 19:00:47 +03:00
|
|
|
for _, cn := range p.conns {
|
2016-03-16 17:57:24 +03:00
|
|
|
if cn == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := p.closeConn(cn); err != nil && retErr == nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
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-03-12 11:52:13 +03:00
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func (p *ConnPool) closeConn(cn *Conn) error {
|
|
|
|
if p.OnClose != nil {
|
|
|
|
_ = p.OnClose(cn)
|
|
|
|
}
|
|
|
|
return cn.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:42:12 +03:00
|
|
|
func (p *ConnPool) ReapStaleConns() (n int, err error) {
|
2016-03-17 19:00:47 +03:00
|
|
|
<-p.queue
|
|
|
|
p.freeConnsMu.Lock()
|
|
|
|
|
|
|
|
if len(p.freeConns) == 0 {
|
|
|
|
p.freeConnsMu.Unlock()
|
|
|
|
p.queue <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var idx int
|
|
|
|
var cn *Conn
|
|
|
|
for idx, cn = range p.freeConns {
|
|
|
|
if !cn.IsStale(p.idleTimeout) {
|
2016-03-12 15:42:12 +03:00
|
|
|
break
|
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
p.connsMu.Lock()
|
|
|
|
p.remove(cn, errConnStale)
|
|
|
|
p.connsMu.Unlock()
|
2016-03-12 15:42:12 +03:00
|
|
|
n++
|
|
|
|
}
|
2016-03-17 19:00:47 +03:00
|
|
|
if idx > 0 {
|
|
|
|
p.freeConns = append(p.freeConns[:0], p.freeConns[idx:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.freeConnsMu.Unlock()
|
|
|
|
p.queue <- struct{}{}
|
2016-03-12 15:42:12 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
for _ = range ticker.C {
|
2016-03-14 14:17:33 +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 13:54:44 +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 13:54:44 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) storeLastErr(err string) {
|
|
|
|
p.lastErr.Store(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) loadLastErr() string {
|
|
|
|
if v := p.lastErr.Load(); v != nil {
|
|
|
|
return v.(string)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
var idleCheckFrequency atomic.Value
|
|
|
|
|
|
|
|
func SetIdleCheckFrequency(d time.Duration) {
|
|
|
|
idleCheckFrequency.Store(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIdleCheckFrequency() time.Duration {
|
|
|
|
v := idleCheckFrequency.Load()
|
|
|
|
if v == nil {
|
|
|
|
return time.Minute
|
|
|
|
}
|
|
|
|
return v.(time.Duration)
|
|
|
|
}
|