redis/pool.go

406 lines
6.6 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis
import (
"container/list"
2013-11-07 18:20:15 +04:00
"errors"
2014-07-31 17:01:54 +04:00
"log"
2013-07-02 15:17:31 +04:00
"net"
"sync"
"time"
2014-06-18 17:47:21 +04:00
"gopkg.in/bufio.v1"
2013-07-02 15:17:31 +04:00
)
2013-11-07 18:20:15 +04:00
var (
errClosed = errors.New("redis: client is closed")
errRateLimited = errors.New("redis: you open connections too fast")
2013-11-07 18:20:15 +04:00
)
2014-05-07 17:03:09 +04:00
var (
zeroTime = time.Time{}
)
2013-07-02 15:17:31 +04:00
type pool interface {
Get() (*conn, bool, error)
Put(*conn) error
Remove(*conn) error
Len() int
2013-11-07 18:20:15 +04:00
Size() int
2013-07-02 15:17:31 +04:00
Close() error
2014-05-11 18:11:55 +04:00
Filter(func(*conn) bool)
2013-07-02 15:17:31 +04:00
}
//------------------------------------------------------------------------------
type conn struct {
2014-05-11 18:11:55 +04:00
netcn net.Conn
2014-06-18 17:47:21 +04:00
rd *bufio.Reader
2014-07-13 16:07:37 +04:00
buf []byte
2014-05-11 18:11:55 +04:00
inUse bool
2013-09-29 12:11:18 +04:00
usedAt time.Time
2013-07-02 15:17:31 +04:00
readTimeout time.Duration
writeTimeout time.Duration
2013-11-07 18:20:15 +04:00
elem *list.Element
2013-07-02 15:17:31 +04:00
}
2013-11-07 18:20:15 +04:00
func newConnFunc(dial func() (net.Conn, error)) func() (*conn, error) {
return func() (*conn, error) {
netcn, err := dial()
if err != nil {
return nil, err
}
cn := &conn{
2014-05-11 18:11:55 +04:00
netcn: netcn,
2014-07-13 16:07:37 +04:00
buf: make([]byte, 0, 64),
2013-11-07 18:20:15 +04:00
}
cn.rd = bufio.NewReader(cn)
return cn, nil
2013-07-02 15:17:31 +04:00
}
}
func (cn *conn) Read(b []byte) (int, error) {
2013-09-11 19:06:36 +04:00
if cn.readTimeout != 0 {
2014-05-11 18:11:55 +04:00
cn.netcn.SetReadDeadline(time.Now().Add(cn.readTimeout))
2013-09-11 20:22:10 +04:00
} else {
2014-05-11 18:11:55 +04:00
cn.netcn.SetReadDeadline(zeroTime)
2013-07-02 15:17:31 +04:00
}
2014-05-11 18:11:55 +04:00
return cn.netcn.Read(b)
2013-07-02 15:17:31 +04:00
}
func (cn *conn) Write(b []byte) (int, error) {
2013-09-11 19:06:36 +04:00
if cn.writeTimeout != 0 {
2014-05-11 18:11:55 +04:00
cn.netcn.SetWriteDeadline(time.Now().Add(cn.writeTimeout))
2013-09-11 20:22:10 +04:00
} else {
2014-05-11 18:11:55 +04:00
cn.netcn.SetWriteDeadline(zeroTime)
2013-07-02 15:17:31 +04:00
}
2014-05-11 18:11:55 +04:00
return cn.netcn.Write(b)
}
func (cn *conn) RemoteAddr() net.Addr {
return cn.netcn.RemoteAddr()
2013-07-02 15:17:31 +04:00
}
2013-11-04 11:53:48 +04:00
func (cn *conn) Close() error {
2014-05-11 18:11:55 +04:00
return cn.netcn.Close()
2013-11-04 11:53:48 +04:00
}
2013-07-02 15:17:31 +04:00
//------------------------------------------------------------------------------
type connPool struct {
dial func() (*conn, error)
rl *rateLimiter
2013-07-02 15:17:31 +04:00
2014-05-11 18:11:55 +04:00
opt *options
2013-07-02 15:17:31 +04:00
cond *sync.Cond
conns *list.List
2014-05-11 18:11:55 +04:00
idleNum int
closed bool
2013-07-02 15:17:31 +04:00
}
2014-05-11 18:11:55 +04:00
func newConnPool(dial func() (*conn, error), opt *options) *connPool {
2013-07-02 15:17:31 +04:00
return &connPool{
dial: dial,
2014-05-11 18:11:55 +04:00
rl: newRateLimiter(time.Second, 2*opt.PoolSize),
opt: opt,
2013-07-02 15:17:31 +04:00
cond: sync.NewCond(&sync.Mutex{}),
conns: list.New(),
}
}
func (p *connPool) new() (*conn, error) {
2014-10-02 12:32:49 +04:00
if !p.rl.Check() {
return nil, errRateLimited
}
return p.dial()
}
2013-07-02 15:17:31 +04:00
func (p *connPool) Get() (*conn, bool, error) {
p.cond.L.Lock()
2013-11-07 18:20:15 +04:00
if p.closed {
p.cond.L.Unlock()
return nil, false, errClosed
2013-07-02 15:17:31 +04:00
}
2014-05-11 18:11:55 +04:00
if p.opt.IdleTimeout > 0 {
for el := p.conns.Front(); el != nil; el = el.Next() {
cn := el.Value.(*conn)
2013-11-07 18:20:15 +04:00
if cn.inUse {
break
}
2014-05-11 18:11:55 +04:00
if time.Since(cn.usedAt) > p.opt.IdleTimeout {
if err := p.remove(cn); err != nil {
2014-07-31 17:01:54 +04:00
log.Printf("remove failed: %s", err)
2013-11-07 18:20:15 +04:00
}
2013-07-02 15:17:31 +04:00
}
}
}
2014-05-11 18:11:55 +04:00
for p.conns.Len() >= p.opt.PoolSize && p.idleNum == 0 {
2013-11-07 18:20:15 +04:00
p.cond.Wait()
}
if p.idleNum > 0 {
2013-11-07 18:20:15 +04:00
elem := p.conns.Front()
cn := elem.Value.(*conn)
if cn.inUse {
panic("pool: precondition failed")
}
cn.inUse = true
p.conns.MoveToBack(elem)
p.idleNum--
2013-11-07 18:20:15 +04:00
p.cond.L.Unlock()
return cn, false, nil
}
2014-05-11 18:11:55 +04:00
if p.conns.Len() < p.opt.PoolSize {
2014-06-28 15:47:37 +04:00
cn, err := p.new()
2013-07-02 15:17:31 +04:00
if err != nil {
2013-11-07 18:20:15 +04:00
p.cond.L.Unlock()
2013-07-02 15:17:31 +04:00
return nil, false, err
}
2013-11-07 18:20:15 +04:00
cn.inUse = true
cn.elem = p.conns.PushBack(cn)
p.cond.L.Unlock()
return cn, true, nil
2013-07-02 15:17:31 +04:00
}
2013-11-07 18:20:15 +04:00
panic("not reached")
2013-07-02 15:17:31 +04:00
}
func (p *connPool) Put(cn *conn) error {
2013-09-29 12:11:18 +04:00
if cn.rd.Buffered() != 0 {
b, _ := cn.rd.ReadN(cn.rd.Buffered())
2014-07-31 17:01:54 +04:00
log.Printf("redis: connection has unread data: %q", b)
return p.Remove(cn)
2013-09-17 13:03:17 +04:00
}
2014-05-11 18:11:55 +04:00
if p.opt.IdleTimeout > 0 {
cn.usedAt = time.Now()
}
2013-07-02 15:17:31 +04:00
p.cond.L.Lock()
2013-11-07 18:20:15 +04:00
if p.closed {
p.cond.L.Unlock()
return errClosed
2013-11-07 18:20:15 +04:00
}
cn.inUse = false
p.conns.MoveToFront(cn.elem)
p.idleNum++
2013-07-02 15:17:31 +04:00
p.cond.Signal()
p.cond.L.Unlock()
2013-07-02 15:17:31 +04:00
return nil
}
func (p *connPool) Remove(cn *conn) error {
2013-11-07 18:20:15 +04:00
p.cond.L.Lock()
if p.closed {
// Noop, connection is already closed.
p.cond.L.Unlock()
return nil
}
err := p.remove(cn)
2013-07-02 15:17:31 +04:00
p.cond.Signal()
p.cond.L.Unlock()
return err
}
func (p *connPool) remove(cn *conn) error {
p.conns.Remove(cn.elem)
cn.elem = nil
if !cn.inUse {
p.idleNum--
}
return cn.Close()
}
// Len returns number of idle connections.
2013-07-02 15:17:31 +04:00
func (p *connPool) Len() int {
2013-09-11 19:06:36 +04:00
defer p.cond.L.Unlock()
p.cond.L.Lock()
return p.idleNum
2013-07-02 15:17:31 +04:00
}
// Size returns number of connections in the pool.
2013-07-02 15:17:31 +04:00
func (p *connPool) Size() int {
2013-09-11 19:06:36 +04:00
defer p.cond.L.Unlock()
p.cond.L.Lock()
2013-11-07 18:20:15 +04:00
return p.conns.Len()
2013-07-02 15:17:31 +04:00
}
2014-05-11 18:11:55 +04:00
func (p *connPool) Filter(f func(*conn) bool) {
p.cond.L.Lock()
for el, next := p.conns.Front(), p.conns.Front(); el != nil; el = next {
next = el.Next()
cn := el.Value.(*conn)
if !f(cn) {
p.remove(cn)
}
}
p.cond.L.Unlock()
}
2013-07-02 15:17:31 +04:00
func (p *connPool) Close() error {
defer p.cond.L.Unlock()
p.cond.L.Lock()
2013-11-07 18:20:15 +04:00
if p.closed {
return nil
}
p.closed = true
2014-10-02 12:32:49 +04:00
p.rl.Close()
2013-11-07 18:20:15 +04:00
var retErr error
2014-05-11 18:11:55 +04:00
for {
e := p.conns.Front()
if e == nil {
break
}
if err := p.remove(e.Value.(*conn)); err != nil {
2014-07-31 17:01:54 +04:00
log.Printf("cn.Close failed: %s", err)
2013-11-07 18:20:15 +04:00
retErr = err
2013-07-02 15:17:31 +04:00
}
}
2013-11-07 18:20:15 +04:00
return retErr
2013-07-02 15:17:31 +04:00
}
//------------------------------------------------------------------------------
type singleConnPool struct {
pool pool
2014-06-28 15:47:37 +04:00
cnMtx sync.Mutex
cn *conn
2013-07-02 15:17:31 +04:00
reusable bool
closed bool
2013-07-02 15:17:31 +04:00
}
2014-06-28 15:47:37 +04:00
func newSingleConnPool(pool pool, reusable bool) *singleConnPool {
2013-07-02 15:17:31 +04:00
return &singleConnPool{
pool: pool,
reusable: reusable,
}
}
2014-06-28 15:47:37 +04:00
func (p *singleConnPool) SetConn(cn *conn) {
p.cnMtx.Lock()
p.cn = cn
p.cnMtx.Unlock()
}
2013-07-02 15:17:31 +04:00
func (p *singleConnPool) Get() (*conn, bool, error) {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
if p.closed {
return nil, false, errClosed
}
2013-07-02 15:17:31 +04:00
if p.cn != nil {
return p.cn, false, nil
}
cn, isNew, err := p.pool.Get()
if err != nil {
return nil, false, err
}
p.cn = cn
2014-06-28 15:47:37 +04:00
return p.cn, isNew, nil
2013-07-02 15:17:31 +04:00
}
func (p *singleConnPool) Put(cn *conn) error {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
2013-07-02 15:17:31 +04:00
if p.cn != cn {
panic("p.cn != cn")
}
if p.closed {
return errClosed
}
2013-07-02 15:17:31 +04:00
return nil
}
2014-06-28 15:47:37 +04:00
func (p *singleConnPool) put() error {
err := p.pool.Put(p.cn)
p.cn = nil
return err
}
2013-07-02 15:17:31 +04:00
func (p *singleConnPool) Remove(cn *conn) error {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
2014-05-11 18:11:55 +04:00
if p.cn == nil {
panic("p.cn == nil")
}
2013-07-02 15:17:31 +04:00
if p.cn != cn {
panic("p.cn != cn")
}
if p.closed {
return errClosed
}
2014-05-11 18:11:55 +04:00
return p.remove()
}
func (p *singleConnPool) remove() error {
err := p.pool.Remove(p.cn)
2013-07-02 15:17:31 +04:00
p.cn = nil
2014-05-11 18:11:55 +04:00
return err
2013-07-02 15:17:31 +04:00
}
func (p *singleConnPool) Len() int {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
2013-07-02 15:17:31 +04:00
if p.cn == nil {
return 0
}
return 1
}
2013-11-07 18:20:15 +04:00
func (p *singleConnPool) Size() int {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
2013-11-07 18:20:15 +04:00
if p.cn == nil {
return 0
}
return 1
}
2014-05-11 18:11:55 +04:00
func (p *singleConnPool) Filter(f func(*conn) bool) {
2014-06-28 15:47:37 +04:00
p.cnMtx.Lock()
2014-05-11 18:11:55 +04:00
if p.cn != nil {
if !f(p.cn) {
p.remove()
}
}
2014-06-28 15:47:37 +04:00
p.cnMtx.Unlock()
2014-05-11 18:11:55 +04:00
}
2013-07-02 15:17:31 +04:00
func (p *singleConnPool) Close() error {
2014-06-28 15:47:37 +04:00
defer p.cnMtx.Unlock()
p.cnMtx.Lock()
if p.closed {
return nil
}
p.closed = true
2013-07-02 15:17:31 +04:00
var err error
if p.cn != nil {
if p.reusable {
2014-06-28 15:47:37 +04:00
err = p.put()
2013-07-02 15:17:31 +04:00
} else {
2014-06-28 15:47:37 +04:00
err = p.remove()
2013-07-02 15:17:31 +04:00
}
}
return err
}