2015-05-14 15:19:29 +03:00
|
|
|
package redis // import "gopkg.in/redis.v3"
|
2012-07-25 17:00:50 +04:00
|
|
|
|
|
|
|
import (
|
2015-05-15 15:21:28 +03:00
|
|
|
"fmt"
|
2014-07-31 17:01:54 +04:00
|
|
|
"log"
|
2016-02-03 20:30:39 +03:00
|
|
|
"os"
|
2016-03-12 11:52:13 +03:00
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"gopkg.in/redis.v3/internal/pool"
|
2012-08-09 14:12:41 +04:00
|
|
|
)
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// Deprecated. Use SetLogger instead.
|
|
|
|
var Logger *log.Logger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetLogger(log.New(os.Stderr, "redis: ", log.LstdFlags))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetLogger(logger *log.Logger) {
|
|
|
|
Logger = logger
|
|
|
|
pool.Logger = logger
|
|
|
|
}
|
2016-02-03 20:30:39 +03:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
type baseClient struct {
|
2016-03-12 11:52:13 +03:00
|
|
|
connPool pool.Pooler
|
2015-05-23 17:55:08 +03:00
|
|
|
opt *Options
|
2016-03-09 16:14:01 +03:00
|
|
|
|
|
|
|
onClose func() error // hook called when client is closed
|
2012-07-26 22:43:21 +04:00
|
|
|
}
|
|
|
|
|
2015-05-15 15:21:28 +03:00
|
|
|
func (c *baseClient) String() string {
|
|
|
|
return fmt.Sprintf("Redis<%s db:%d>", c.opt.Addr, c.opt.DB)
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (c *baseClient) conn() (*pool.Conn, bool, error) {
|
2016-03-12 13:41:02 +03:00
|
|
|
cn, isNew, err := c.connPool.Get()
|
|
|
|
if err == nil && isNew {
|
|
|
|
err = c.initConn(cn)
|
|
|
|
if err != nil {
|
|
|
|
c.putConn(cn, err, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cn, isNew, err
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func (c *baseClient) putConn(cn *pool.Conn, err error, allowTimeout bool) bool {
|
2016-03-08 18:18:52 +03:00
|
|
|
if isBadConn(err, allowTimeout) {
|
2016-03-12 13:41:02 +03:00
|
|
|
err = c.connPool.Replace(cn, err)
|
2016-03-01 13:31:06 +03:00
|
|
|
if err != nil {
|
2016-03-02 14:37:28 +03:00
|
|
|
Logger.Printf("pool.Remove failed: %s", err)
|
2016-03-01 13:31:06 +03:00
|
|
|
}
|
|
|
|
return false
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
2016-03-01 13:31:06 +03:00
|
|
|
|
|
|
|
err = c.connPool.Put(cn)
|
2015-04-17 16:18:44 +03:00
|
|
|
if err != nil {
|
2016-03-02 14:37:28 +03:00
|
|
|
Logger.Printf("pool.Put failed: %s", err)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
2016-03-01 13:31:06 +03:00
|
|
|
return true
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 13:41:02 +03:00
|
|
|
func (c *baseClient) initConn(cn *pool.Conn) error {
|
|
|
|
if c.opt.Password == "" && c.opt.DB == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temp client for Auth and Select.
|
|
|
|
client := newClient(c.opt, pool.NewSingleConnPool(cn))
|
|
|
|
|
|
|
|
if c.opt.Password != "" {
|
|
|
|
if err := client.Auth(c.opt.Password).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.opt.DB > 0 {
|
|
|
|
if err := client.Select(c.opt.DB).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-24 15:12:48 +03:00
|
|
|
func (c *baseClient) process(cmd Cmder) {
|
2015-05-10 15:33:04 +03:00
|
|
|
for i := 0; i <= c.opt.MaxRetries; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
cmd.reset()
|
|
|
|
}
|
2012-07-26 22:43:21 +04:00
|
|
|
|
2015-11-14 15:44:16 +03:00
|
|
|
cn, _, err := c.conn()
|
2015-05-10 15:33:04 +03:00
|
|
|
if err != nil {
|
|
|
|
cmd.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
2012-07-26 22:43:21 +04:00
|
|
|
|
2016-03-08 18:18:52 +03:00
|
|
|
readTimeout := cmd.readTimeout()
|
|
|
|
if readTimeout != nil {
|
|
|
|
cn.ReadTimeout = *readTimeout
|
2015-05-10 15:33:04 +03:00
|
|
|
} else {
|
|
|
|
cn.ReadTimeout = c.opt.ReadTimeout
|
|
|
|
}
|
2016-03-08 18:18:52 +03:00
|
|
|
cn.WriteTimeout = c.opt.WriteTimeout
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
if err := writeCmd(cn, cmd); err != nil {
|
2016-03-08 18:18:52 +03:00
|
|
|
c.putConn(cn, err, false)
|
2015-05-10 15:33:04 +03:00
|
|
|
cmd.setErr(err)
|
|
|
|
if shouldRetry(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
err = cmd.readReply(cn)
|
2016-03-08 18:18:52 +03:00
|
|
|
c.putConn(cn, err, readTimeout != nil)
|
2015-05-10 15:33:04 +03:00
|
|
|
if shouldRetry(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2012-07-26 19:16:17 +04:00
|
|
|
return
|
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the client, releasing any open resources.
|
2015-09-12 09:36:03 +03:00
|
|
|
//
|
|
|
|
// It is rare to Close a Client, as the Client is meant to be
|
|
|
|
// long-lived and shared between many goroutines.
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) Close() error {
|
2016-03-09 16:14:01 +03:00
|
|
|
var retErr error
|
|
|
|
if c.onClose != nil {
|
|
|
|
if err := c.onClose(); err != nil && retErr == nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.connPool.Close(); err != nil && retErr == nil {
|
|
|
|
retErr = err
|
|
|
|
}
|
|
|
|
return retErr
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// Client is a Redis client representing a pool of zero or more
|
|
|
|
// underlying connections. It's safe for concurrent use by multiple
|
|
|
|
// goroutines.
|
2012-08-11 18:42:10 +04:00
|
|
|
type Client struct {
|
2016-03-09 12:49:05 +03:00
|
|
|
baseClient
|
2015-01-24 15:12:48 +03:00
|
|
|
commandable
|
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func newClient(opt *Options, pool pool.Pooler) *Client {
|
2016-03-09 12:49:05 +03:00
|
|
|
base := baseClient{opt: opt, connPool: pool}
|
2015-01-24 15:12:48 +03:00
|
|
|
return &Client{
|
2016-03-09 16:14:01 +03:00
|
|
|
baseClient: base,
|
|
|
|
commandable: commandable{
|
|
|
|
process: base.process,
|
|
|
|
},
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// NewClient returns a client to the Redis Server specified by Options.
|
2015-05-23 17:55:08 +03:00
|
|
|
func NewClient(opt *Options) *Client {
|
2016-03-12 11:52:13 +03:00
|
|
|
return newClient(opt, newConnPool(opt))
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2016-01-19 19:36:40 +03:00
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
// PoolStats returns connection pool stats.
|
2016-01-19 19:36:40 +03:00
|
|
|
func (c *Client) PoolStats() *PoolStats {
|
2016-03-12 11:52:13 +03:00
|
|
|
s := c.connPool.Stats()
|
|
|
|
return &PoolStats{
|
|
|
|
Requests: atomic.LoadUint32(&s.Requests),
|
|
|
|
Hits: atomic.LoadUint32(&s.Hits),
|
|
|
|
Waits: atomic.LoadUint32(&s.Waits),
|
|
|
|
Timeouts: atomic.LoadUint32(&s.Timeouts),
|
|
|
|
|
|
|
|
TotalConns: atomic.LoadUint32(&s.TotalConns),
|
|
|
|
FreeConns: atomic.LoadUint32(&s.FreeConns),
|
|
|
|
}
|
2016-01-19 19:36:40 +03:00
|
|
|
}
|