mtfosbot/vendor/github.com/gomodule/redigo/redis/pool.go

563 lines
14 KiB
Go
Raw Normal View History

2018-09-02 01:15:35 +00:00
// Copyright 2012 Gary Burd
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package redis
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"errors"
"io"
"strconv"
"sync"
"sync/atomic"
"time"
2018-11-16 03:10:19 +00:00
"github.com/gomodule/redigo/internal"
2018-09-02 01:15:35 +00:00
)
var (
2018-11-16 03:10:19 +00:00
_ ConnWithTimeout = (*activeConn)(nil)
_ ConnWithTimeout = (*errorConn)(nil)
2018-09-02 01:15:35 +00:00
)
var nowFunc = time.Now // for testing
// ErrPoolExhausted is returned from a pool connection method (Do, Send,
// Receive, Flush, Err) when the maximum number of database connections in the
// pool has been reached.
var ErrPoolExhausted = errors.New("redigo: connection pool exhausted")
var (
errPoolClosed = errors.New("redigo: connection pool closed")
errConnClosed = errors.New("redigo: connection closed")
)
// Pool maintains a pool of connections. The application calls the Get method
// to get a connection from the pool and the connection's Close method to
// return the connection's resources to the pool.
//
// The following example shows how to use a pool in a web application. The
// application creates a pool at application startup and makes it available to
// request handlers using a package level variable. The pool configuration used
// here is an example, not a recommendation.
//
// func newPool(addr string) *redis.Pool {
// return &redis.Pool{
// MaxIdle: 3,
// IdleTimeout: 240 * time.Second,
// Dial: func () (redis.Conn, error) { return redis.Dial("tcp", addr) },
// }
// }
//
// var (
// pool *redis.Pool
// redisServer = flag.String("redisServer", ":6379", "")
// )
//
// func main() {
// flag.Parse()
// pool = newPool(*redisServer)
// ...
// }
//
// A request handler gets a connection from the pool and closes the connection
// when the handler is done:
//
// func serveHome(w http.ResponseWriter, r *http.Request) {
// conn := pool.Get()
// defer conn.Close()
// ...
// }
//
// Use the Dial function to authenticate connections with the AUTH command or
// select a database with the SELECT command:
//
// pool := &redis.Pool{
// // Other pool configuration not shown in this example.
// Dial: func () (redis.Conn, error) {
// c, err := redis.Dial("tcp", server)
// if err != nil {
// return nil, err
// }
// if _, err := c.Do("AUTH", password); err != nil {
// c.Close()
// return nil, err
// }
// if _, err := c.Do("SELECT", db); err != nil {
// c.Close()
// return nil, err
// }
// return c, nil
// },
// }
//
// Use the TestOnBorrow function to check the health of an idle connection
// before the connection is returned to the application. This example PINGs
// connections that have been idle more than a minute:
//
// pool := &redis.Pool{
// // Other pool configuration not shown in this example.
// TestOnBorrow: func(c redis.Conn, t time.Time) error {
// if time.Since(t) < time.Minute {
// return nil
// }
// _, err := c.Do("PING")
// return err
// },
// }
//
type Pool struct {
// Dial is an application supplied function for creating and configuring a
// connection.
//
// The connection returned from Dial must not be in a special state
// (subscribed to pubsub channel, transaction started, ...).
Dial func() (Conn, error)
// TestOnBorrow is an optional application supplied function for checking
// the health of an idle connection before the connection is used again by
// the application. Argument t is the time that the connection was returned
// to the pool. If the function returns an error, then the connection is
// closed.
TestOnBorrow func(c Conn, t time.Time) error
// Maximum number of idle connections in the pool.
MaxIdle int
// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActive int
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout time.Duration
// If Wait is true and the pool is at the MaxActive limit, then Get() waits
// for a connection to be returned to the pool before returning.
Wait bool
2018-11-16 03:10:19 +00:00
// Close connections older than this duration. If the value is zero, then
// the pool does not close connections based on age.
MaxConnLifetime time.Duration
2018-09-02 01:15:35 +00:00
chInitialized uint32 // set to 1 when field ch is initialized
mu sync.Mutex // mu protects the following fields
closed bool // set to true when the pool is closed.
active int // the number of open connections in the pool
ch chan struct{} // limits open connections when p.Wait is true
idle idleList // idle connections
}
// NewPool creates a new pool.
//
// Deprecated: Initialize the Pool directory as shown in the example.
func NewPool(newFn func() (Conn, error), maxIdle int) *Pool {
return &Pool{Dial: newFn, MaxIdle: maxIdle}
}
// Get gets a connection. The application must close the returned connection.
// This method always returns a valid connection so that applications can defer
// error handling to the first use of the connection. If there is an error
// getting an underlying connection, then the connection Err, Do, Send, Flush
// and Receive methods return that error.
func (p *Pool) Get() Conn {
2018-11-16 03:10:19 +00:00
pc, err := p.get(nil)
2018-09-02 01:15:35 +00:00
if err != nil {
2018-11-16 03:10:19 +00:00
return errorConn{err}
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
return &activeConn{p: p, pc: pc}
2018-09-02 01:15:35 +00:00
}
// PoolStats contains pool statistics.
type PoolStats struct {
// ActiveCount is the number of connections in the pool. The count includes
// idle connections and connections in use.
ActiveCount int
// IdleCount is the number of idle connections in the pool.
IdleCount int
}
// Stats returns pool's statistics.
func (p *Pool) Stats() PoolStats {
p.mu.Lock()
stats := PoolStats{
ActiveCount: p.active,
IdleCount: p.idle.count,
}
p.mu.Unlock()
return stats
}
// ActiveCount returns the number of connections in the pool. The count
// includes idle connections and connections in use.
func (p *Pool) ActiveCount() int {
p.mu.Lock()
active := p.active
p.mu.Unlock()
return active
}
// IdleCount returns the number of idle connections in the pool.
func (p *Pool) IdleCount() int {
p.mu.Lock()
idle := p.idle.count
p.mu.Unlock()
return idle
}
// Close releases the resources used by the pool.
func (p *Pool) Close() error {
p.mu.Lock()
if p.closed {
p.mu.Unlock()
return nil
}
p.closed = true
p.active -= p.idle.count
2018-11-16 03:10:19 +00:00
pc := p.idle.front
2018-09-02 01:15:35 +00:00
p.idle.count = 0
p.idle.front, p.idle.back = nil, nil
if p.ch != nil {
close(p.ch)
}
p.mu.Unlock()
2018-11-16 03:10:19 +00:00
for ; pc != nil; pc = pc.next {
pc.c.Close()
2018-09-02 01:15:35 +00:00
}
return nil
}
func (p *Pool) lazyInit() {
// Fast path.
if atomic.LoadUint32(&p.chInitialized) == 1 {
return
}
// Slow path.
p.mu.Lock()
if p.chInitialized == 0 {
p.ch = make(chan struct{}, p.MaxActive)
if p.closed {
close(p.ch)
} else {
for i := 0; i < p.MaxActive; i++ {
p.ch <- struct{}{}
}
}
atomic.StoreUint32(&p.chInitialized, 1)
}
p.mu.Unlock()
}
// get prunes stale connections and returns a connection from the idle list or
// creates a new connection.
func (p *Pool) get(ctx interface {
Done() <-chan struct{}
Err() error
2018-11-16 03:10:19 +00:00
}) (*poolConn, error) {
2018-09-02 01:15:35 +00:00
// Handle limit for p.Wait == true.
if p.Wait && p.MaxActive > 0 {
p.lazyInit()
if ctx == nil {
<-p.ch
} else {
select {
case <-p.ch:
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
p.mu.Lock()
// Prune stale connections at the back of the idle list.
if p.IdleTimeout > 0 {
n := p.idle.count
for i := 0; i < n && p.idle.back != nil && p.idle.back.t.Add(p.IdleTimeout).Before(nowFunc()); i++ {
2018-11-16 03:10:19 +00:00
pc := p.idle.back
2018-09-02 01:15:35 +00:00
p.idle.popBack()
p.mu.Unlock()
2018-11-16 03:10:19 +00:00
pc.c.Close()
2018-09-02 01:15:35 +00:00
p.mu.Lock()
p.active--
}
}
// Get idle connection from the front of idle list.
for p.idle.front != nil {
2018-11-16 03:10:19 +00:00
pc := p.idle.front
2018-09-02 01:15:35 +00:00
p.idle.popFront()
p.mu.Unlock()
2018-11-16 03:10:19 +00:00
if (p.TestOnBorrow == nil || p.TestOnBorrow(pc.c, pc.t) == nil) &&
(p.MaxConnLifetime == 0 || nowFunc().Sub(pc.created) < p.MaxConnLifetime) {
return pc, nil
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
pc.c.Close()
2018-09-02 01:15:35 +00:00
p.mu.Lock()
p.active--
}
// Check for pool closed before dialing a new connection.
if p.closed {
p.mu.Unlock()
return nil, errors.New("redigo: get on closed pool")
}
// Handle limit for p.Wait == false.
if !p.Wait && p.MaxActive > 0 && p.active >= p.MaxActive {
p.mu.Unlock()
return nil, ErrPoolExhausted
}
p.active++
p.mu.Unlock()
c, err := p.Dial()
if err != nil {
c = nil
p.mu.Lock()
p.active--
if p.ch != nil && !p.closed {
p.ch <- struct{}{}
}
p.mu.Unlock()
}
2018-11-16 03:10:19 +00:00
return &poolConn{c: c, created: nowFunc()}, err
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
func (p *Pool) put(pc *poolConn, forceClose bool) error {
2018-09-02 01:15:35 +00:00
p.mu.Lock()
if !p.closed && !forceClose {
2018-11-16 03:10:19 +00:00
pc.t = nowFunc()
p.idle.pushFront(pc)
2018-09-02 01:15:35 +00:00
if p.idle.count > p.MaxIdle {
2018-11-16 03:10:19 +00:00
pc = p.idle.back
2018-09-02 01:15:35 +00:00
p.idle.popBack()
} else {
2018-11-16 03:10:19 +00:00
pc = nil
2018-09-02 01:15:35 +00:00
}
}
2018-11-16 03:10:19 +00:00
if pc != nil {
2018-09-02 01:15:35 +00:00
p.mu.Unlock()
2018-11-16 03:10:19 +00:00
pc.c.Close()
2018-09-02 01:15:35 +00:00
p.mu.Lock()
p.active--
}
if p.ch != nil && !p.closed {
p.ch <- struct{}{}
}
p.mu.Unlock()
return nil
}
2018-11-16 03:10:19 +00:00
type activeConn struct {
2018-09-02 01:15:35 +00:00
p *Pool
2018-11-16 03:10:19 +00:00
pc *poolConn
2018-09-02 01:15:35 +00:00
state int
}
var (
sentinel []byte
sentinelOnce sync.Once
)
func initSentinel() {
p := make([]byte, 64)
if _, err := rand.Read(p); err == nil {
sentinel = p
} else {
h := sha1.New()
io.WriteString(h, "Oops, rand failed. Use time instead.")
io.WriteString(h, strconv.FormatInt(time.Now().UnixNano(), 10))
sentinel = h.Sum(nil)
}
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Close() error {
pc := ac.pc
if pc == nil {
2018-09-02 01:15:35 +00:00
return nil
}
2018-11-16 03:10:19 +00:00
ac.pc = nil
if ac.state&internal.MultiState != 0 {
pc.c.Send("DISCARD")
ac.state &^= (internal.MultiState | internal.WatchState)
} else if ac.state&internal.WatchState != 0 {
pc.c.Send("UNWATCH")
ac.state &^= internal.WatchState
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
if ac.state&internal.SubscribeState != 0 {
pc.c.Send("UNSUBSCRIBE")
pc.c.Send("PUNSUBSCRIBE")
2018-09-02 01:15:35 +00:00
// To detect the end of the message stream, ask the server to echo
// a sentinel value and read until we see that value.
sentinelOnce.Do(initSentinel)
2018-11-16 03:10:19 +00:00
pc.c.Send("ECHO", sentinel)
pc.c.Flush()
2018-09-02 01:15:35 +00:00
for {
2018-11-16 03:10:19 +00:00
p, err := pc.c.Receive()
2018-09-02 01:15:35 +00:00
if err != nil {
break
}
if p, ok := p.([]byte); ok && bytes.Equal(p, sentinel) {
2018-11-16 03:10:19 +00:00
ac.state &^= internal.SubscribeState
2018-09-02 01:15:35 +00:00
break
}
}
}
2018-11-16 03:10:19 +00:00
pc.c.Do("")
ac.p.put(pc, ac.state != 0 || pc.c.Err() != nil)
2018-09-02 01:15:35 +00:00
return nil
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Err() error {
pc := ac.pc
if pc == nil {
return errConnClosed
}
2018-09-02 01:15:35 +00:00
return pc.c.Err()
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
pc := ac.pc
if pc == nil {
return nil, errConnClosed
}
2018-09-02 01:15:35 +00:00
ci := internal.LookupCommandInfo(commandName)
2018-11-16 03:10:19 +00:00
ac.state = (ac.state | ci.Set) &^ ci.Clear
2018-09-02 01:15:35 +00:00
return pc.c.Do(commandName, args...)
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (reply interface{}, err error) {
pc := ac.pc
if pc == nil {
return nil, errConnClosed
}
2018-09-02 01:15:35 +00:00
cwt, ok := pc.c.(ConnWithTimeout)
if !ok {
return nil, errTimeoutNotSupported
}
ci := internal.LookupCommandInfo(commandName)
2018-11-16 03:10:19 +00:00
ac.state = (ac.state | ci.Set) &^ ci.Clear
2018-09-02 01:15:35 +00:00
return cwt.DoWithTimeout(timeout, commandName, args...)
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Send(commandName string, args ...interface{}) error {
pc := ac.pc
if pc == nil {
return errConnClosed
}
2018-09-02 01:15:35 +00:00
ci := internal.LookupCommandInfo(commandName)
2018-11-16 03:10:19 +00:00
ac.state = (ac.state | ci.Set) &^ ci.Clear
2018-09-02 01:15:35 +00:00
return pc.c.Send(commandName, args...)
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Flush() error {
pc := ac.pc
if pc == nil {
return errConnClosed
}
2018-09-02 01:15:35 +00:00
return pc.c.Flush()
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) Receive() (reply interface{}, err error) {
pc := ac.pc
if pc == nil {
return nil, errConnClosed
}
2018-09-02 01:15:35 +00:00
return pc.c.Receive()
}
2018-11-16 03:10:19 +00:00
func (ac *activeConn) ReceiveWithTimeout(timeout time.Duration) (reply interface{}, err error) {
pc := ac.pc
if pc == nil {
return nil, errConnClosed
}
2018-09-02 01:15:35 +00:00
cwt, ok := pc.c.(ConnWithTimeout)
if !ok {
return nil, errTimeoutNotSupported
}
return cwt.ReceiveWithTimeout(timeout)
}
2018-11-16 03:10:19 +00:00
type errorConn struct{ err error }
2018-09-02 01:15:35 +00:00
2018-11-16 03:10:19 +00:00
func (ec errorConn) Do(string, ...interface{}) (interface{}, error) { return nil, ec.err }
func (ec errorConn) DoWithTimeout(time.Duration, string, ...interface{}) (interface{}, error) {
2018-09-02 01:15:35 +00:00
return nil, ec.err
}
2018-11-16 03:10:19 +00:00
func (ec errorConn) Send(string, ...interface{}) error { return ec.err }
func (ec errorConn) Err() error { return ec.err }
func (ec errorConn) Close() error { return nil }
func (ec errorConn) Flush() error { return ec.err }
func (ec errorConn) Receive() (interface{}, error) { return nil, ec.err }
func (ec errorConn) ReceiveWithTimeout(time.Duration) (interface{}, error) { return nil, ec.err }
2018-09-02 01:15:35 +00:00
type idleList struct {
count int
2018-11-16 03:10:19 +00:00
front, back *poolConn
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
type poolConn struct {
2018-09-02 01:15:35 +00:00
c Conn
t time.Time
2018-11-16 03:10:19 +00:00
created time.Time
next, prev *poolConn
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
func (l *idleList) pushFront(pc *poolConn) {
pc.next = l.front
pc.prev = nil
2018-09-02 01:15:35 +00:00
if l.count == 0 {
2018-11-16 03:10:19 +00:00
l.back = pc
2018-09-02 01:15:35 +00:00
} else {
2018-11-16 03:10:19 +00:00
l.front.prev = pc
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
l.front = pc
2018-09-02 01:15:35 +00:00
l.count++
return
}
func (l *idleList) popFront() {
2018-11-16 03:10:19 +00:00
pc := l.front
2018-09-02 01:15:35 +00:00
l.count--
if l.count == 0 {
l.front, l.back = nil, nil
} else {
2018-11-16 03:10:19 +00:00
pc.next.prev = nil
l.front = pc.next
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
pc.next, pc.prev = nil, nil
2018-09-02 01:15:35 +00:00
}
func (l *idleList) popBack() {
2018-11-16 03:10:19 +00:00
pc := l.back
2018-09-02 01:15:35 +00:00
l.count--
if l.count == 0 {
l.front, l.back = nil, nil
} else {
2018-11-16 03:10:19 +00:00
pc.prev.next = nil
l.back = pc.prev
2018-09-02 01:15:35 +00:00
}
2018-11-16 03:10:19 +00:00
pc.next, pc.prev = nil, nil
2018-09-02 01:15:35 +00:00
}