2016-03-12 11:52:13 +03:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-03-12 13:41:02 +03:00
|
|
|
"net"
|
2016-03-12 11:52:13 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/bsm/ratelimit.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Logger *log.Logger
|
|
|
|
|
|
|
|
var (
|
2016-03-14 14:17:33 +03:00
|
|
|
ErrClosed = errors.New("redis: client is closed")
|
|
|
|
errConnClosed = errors.New("redis: connection is closed")
|
2016-03-12 11:52:13 +03:00
|
|
|
ErrPoolTimeout = errors.New("redis: connection pool timeout")
|
|
|
|
)
|
|
|
|
|
|
|
|
// PoolStats contains pool state information and accumulated stats.
|
|
|
|
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 {
|
|
|
|
First() *Conn
|
|
|
|
Get() (*Conn, bool, error)
|
|
|
|
Put(*Conn) error
|
2016-03-12 13:41:02 +03:00
|
|
|
Replace(*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-12 11:52:13 +03:00
|
|
|
|
|
|
|
poolTimeout time.Duration
|
|
|
|
idleTimeout time.Duration
|
|
|
|
|
|
|
|
conns *connList
|
2016-03-12 14:39:50 +03:00
|
|
|
freeConns *connStack
|
2016-03-12 11:52:13 +03:00
|
|
|
stats PoolStats
|
|
|
|
|
|
|
|
_closed int32
|
|
|
|
|
|
|
|
lastErr atomic.Value
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
var _ Pooler = (*ConnPool)(nil)
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func NewConnPool(dial dialer, poolSize int, poolTimeout, idleTimeout time.Duration) *ConnPool {
|
|
|
|
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,
|
|
|
|
|
|
|
|
conns: newConnList(poolSize),
|
2016-03-12 14:39:50 +03:00
|
|
|
freeConns: newConnStack(poolSize),
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
if idleTimeout > 0 {
|
|
|
|
go p.reaper()
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2016-03-14 14:17:33 +03:00
|
|
|
func (p *ConnPool) Closed() bool {
|
2016-03-12 11:52:13 +03:00
|
|
|
return atomic.LoadInt32(&p._closed) == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) isIdle(cn *Conn) bool {
|
|
|
|
return p.idleTimeout > 0 && time.Since(cn.UsedAt) > p.idleTimeout
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:42:12 +03:00
|
|
|
func (p *ConnPool) Add(cn *Conn) bool {
|
|
|
|
if !p.conns.Reserve() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
p.conns.Add(cn)
|
|
|
|
p.Put(cn)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// First returns first non-idle connection from the pool or nil if
|
|
|
|
// there are no connections.
|
|
|
|
func (p *ConnPool) First() *Conn {
|
|
|
|
for {
|
2016-03-12 14:39:50 +03:00
|
|
|
cn := p.freeConns.Pop()
|
|
|
|
if cn != nil && cn.IsStale(p.idleTimeout) {
|
|
|
|
var err error
|
|
|
|
cn, err = p.replace(cn)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("pool.replace failed: %s", err)
|
|
|
|
continue
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 14:39:50 +03:00
|
|
|
return cn
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait waits for free non-idle connection. It returns nil on timeout.
|
|
|
|
func (p *ConnPool) wait() *Conn {
|
|
|
|
for {
|
2016-03-12 14:39:50 +03:00
|
|
|
cn := p.freeConns.PopWithTimeout(p.poolTimeout)
|
|
|
|
if cn != nil && cn.IsStale(p.idleTimeout) {
|
|
|
|
var err error
|
|
|
|
cn, err = p.replace(cn)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Printf("pool.replace failed: %s", err)
|
|
|
|
continue
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 14:39:50 +03:00
|
|
|
return cn
|
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-12 13:41:02 +03:00
|
|
|
func (p *ConnPool) newConn() (*Conn, error) {
|
|
|
|
netConn, err := p.dial()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewConn(netConn), nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Get returns existed connection from the pool or creates a new one.
|
|
|
|
func (p *ConnPool) Get() (cn *Conn, isNew bool, err error) {
|
2016-03-14 14:17:33 +03:00
|
|
|
if p.Closed() {
|
|
|
|
err = ErrClosed
|
2016-03-12 11:52:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint32(&p.stats.Requests, 1)
|
|
|
|
|
|
|
|
// Fetch first non-idle connection, if available.
|
|
|
|
if cn = p.First(); cn != nil {
|
|
|
|
atomic.AddUint32(&p.stats.Hits, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create a new one.
|
|
|
|
if p.conns.Reserve() {
|
|
|
|
isNew = true
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
cn, err = p.newConn()
|
2016-03-12 11:52:13 +03:00
|
|
|
if err != nil {
|
2016-03-14 14:17:33 +03:00
|
|
|
p.conns.CancelReservation()
|
2016-03-12 11:52:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.conns.Add(cn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, wait for the available connection.
|
|
|
|
atomic.AddUint32(&p.stats.Waits, 1)
|
|
|
|
if cn = p.wait(); cn != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint32(&p.stats.Timeouts, 1)
|
|
|
|
err = ErrPoolTimeout
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
Logger.Print(err)
|
2016-03-12 13:41:02 +03:00
|
|
|
return p.Replace(cn, err)
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
2016-03-12 14:39:50 +03:00
|
|
|
p.freeConns.Push(cn)
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) replace(cn *Conn) (*Conn, error) {
|
2016-03-14 14:17:33 +03:00
|
|
|
idx := cn.Close()
|
|
|
|
if idx == -1 {
|
|
|
|
return nil, errConnClosed
|
|
|
|
}
|
2016-03-12 13:41:02 +03:00
|
|
|
|
|
|
|
netConn, err := p.dial()
|
2016-03-12 11:52:13 +03:00
|
|
|
if err != nil {
|
2016-03-14 14:17:33 +03:00
|
|
|
p.conns.Remove(idx)
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-14 14:17:33 +03:00
|
|
|
|
|
|
|
cn = NewConn(netConn)
|
|
|
|
cn.SetIndex(idx)
|
|
|
|
p.conns.Replace(cn)
|
2016-03-12 13:41:02 +03:00
|
|
|
|
|
|
|
return cn, nil
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
func (p *ConnPool) Replace(cn *Conn, reason error) error {
|
2016-03-12 11:52:13 +03:00
|
|
|
p.storeLastErr(reason.Error())
|
|
|
|
|
|
|
|
// Replace existing connection with new one and unblock waiter.
|
|
|
|
newcn, err := p.replace(cn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-12 14:39:50 +03:00
|
|
|
p.freeConns.Push(newcn)
|
2016-03-12 11:52:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:42:12 +03:00
|
|
|
func (p *ConnPool) Remove(cn *Conn, reason error) error {
|
2016-03-14 14:17:33 +03:00
|
|
|
idx := cn.Close()
|
|
|
|
if idx == -1 {
|
|
|
|
return errConnClosed
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:42:12 +03:00
|
|
|
p.storeLastErr(reason.Error())
|
2016-03-14 14:17:33 +03:00
|
|
|
p.conns.Remove(idx)
|
|
|
|
return nil
|
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 {
|
|
|
|
return p.conns.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeLen returns number of free connections.
|
|
|
|
func (p *ConnPool) FreeLen() int {
|
2016-03-12 14:39:50 +03:00
|
|
|
return p.freeConns.Len()
|
2016-03-12 11:52:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ConnPool) Stats() *PoolStats {
|
|
|
|
stats := p.stats
|
|
|
|
stats.Requests = atomic.LoadUint32(&p.stats.Requests)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
// Wait for app to free connections, but don't close them immediately.
|
|
|
|
for i := 0; i < p.Len(); i++ {
|
|
|
|
if cn := p.wait(); cn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close all connections.
|
|
|
|
if err := p.conns.Close(); err != nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
2016-03-12 15:42:12 +03:00
|
|
|
func (p *ConnPool) ReapStaleConns() (n int, err error) {
|
|
|
|
for {
|
|
|
|
cn := p.freeConns.ShiftStale(p.idleTimeout)
|
|
|
|
if cn == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err = p.Remove(cn, errors.New("connection is stale")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (p *ConnPool) reaper() {
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
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 {
|
|
|
|
Logger.Printf("ReapStaleConns failed: %s", err)
|
|
|
|
} else if n > 0 {
|
|
|
|
Logger.Printf("removed %d stale connections", n)
|
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 ""
|
|
|
|
}
|