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"
|
2012-08-05 16:09:43 +04:00
|
|
|
"net"
|
2012-10-26 19:21:14 +04:00
|
|
|
"time"
|
2012-08-09 14:12:41 +04:00
|
|
|
)
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
type baseClient struct {
|
|
|
|
connPool pool
|
2015-05-23 17:55:08 +03:00
|
|
|
opt *Options
|
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)
|
|
|
|
}
|
|
|
|
|
2015-11-14 15:44:16 +03:00
|
|
|
func (c *baseClient) conn() (*conn, bool, error) {
|
2015-04-22 10:32:54 +03:00
|
|
|
return c.connPool.Get()
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2015-12-22 16:45:03 +03:00
|
|
|
func (c *baseClient) putConn(cn *conn, err error) {
|
|
|
|
if isBadConn(cn, err) {
|
|
|
|
err = c.connPool.Remove(cn, err)
|
2015-04-17 16:18:44 +03:00
|
|
|
} else {
|
2015-11-14 15:44:16 +03:00
|
|
|
err = c.connPool.Put(cn)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
2015-04-17 16:18:44 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("redis: putConn failed: %s", err)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-10 15:33:04 +03:00
|
|
|
if timeout := cmd.writeTimeout(); timeout != nil {
|
|
|
|
cn.WriteTimeout = *timeout
|
|
|
|
} else {
|
|
|
|
cn.WriteTimeout = c.opt.WriteTimeout
|
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2015-05-10 15:33:04 +03:00
|
|
|
if timeout := cmd.readTimeout(); timeout != nil {
|
|
|
|
cn.ReadTimeout = *timeout
|
|
|
|
} else {
|
|
|
|
cn.ReadTimeout = c.opt.ReadTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cn.writeCmds(cmd); err != nil {
|
|
|
|
c.putConn(cn, err)
|
|
|
|
cmd.setErr(err)
|
|
|
|
if shouldRetry(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:09:20 +03:00
|
|
|
err = cmd.readReply(cn)
|
2015-04-17 16:18:44 +03:00
|
|
|
c.putConn(cn, err)
|
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 {
|
|
|
|
return c.connPool.Close()
|
2012-07-26 19:16:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Options struct {
|
2015-05-23 17:55:08 +03:00
|
|
|
// The network type, either tcp or unix.
|
|
|
|
// Default is tcp.
|
2014-09-30 12:46:56 +04:00
|
|
|
Network string
|
2015-05-23 17:55:08 +03:00
|
|
|
// host:port address.
|
2015-01-31 17:54:37 +03:00
|
|
|
Addr string
|
2014-09-30 12:46:56 +04:00
|
|
|
|
|
|
|
// Dialer creates new network connection and has priority over
|
|
|
|
// Network and Addr options.
|
|
|
|
Dialer func() (net.Conn, error)
|
|
|
|
|
2015-01-31 17:54:37 +03:00
|
|
|
// An optional password. Must match the password specified in the
|
2015-05-23 17:55:08 +03:00
|
|
|
// requirepass server configuration option.
|
2014-05-11 11:42:40 +04:00
|
|
|
Password string
|
2015-05-23 17:55:08 +03:00
|
|
|
// A database to be selected after connecting to server.
|
2015-01-31 17:54:37 +03:00
|
|
|
DB int64
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
// The maximum number of retries before giving up.
|
|
|
|
// Default is to not retry failed commands.
|
|
|
|
MaxRetries int
|
|
|
|
|
2015-01-31 17:54:37 +03:00
|
|
|
// Sets the deadline for establishing new connections. If reached,
|
2015-05-23 17:55:08 +03:00
|
|
|
// dial will fail with a timeout.
|
2015-01-31 17:54:37 +03:00
|
|
|
DialTimeout time.Duration
|
|
|
|
// Sets the deadline for socket reads. If reached, commands will
|
|
|
|
// fail with a timeout instead of blocking.
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
// Sets the deadline for socket writes. If reached, commands will
|
|
|
|
// fail with a timeout instead of blocking.
|
2014-05-11 11:42:40 +04:00
|
|
|
WriteTimeout time.Duration
|
2014-05-11 18:11:55 +04:00
|
|
|
|
2015-01-31 17:54:37 +03:00
|
|
|
// The maximum number of socket connections.
|
2015-05-23 17:55:08 +03:00
|
|
|
// Default is 10 connections.
|
2015-01-31 17:54:37 +03:00
|
|
|
PoolSize int
|
2015-05-23 17:55:08 +03:00
|
|
|
// Specifies amount of time client waits for connection if all
|
|
|
|
// connections are busy before returning an error.
|
2015-11-18 17:35:02 +03:00
|
|
|
// Default is 1 seconds.
|
2015-01-31 16:20:37 +03:00
|
|
|
PoolTimeout time.Duration
|
2015-05-23 17:55:08 +03:00
|
|
|
// Specifies amount of time after which client closes idle
|
|
|
|
// connections. Should be less than server's timeout.
|
|
|
|
// Default is to not close idle connections.
|
2014-05-11 18:11:55 +04:00
|
|
|
IdleTimeout time.Duration
|
2015-05-23 17:55:08 +03:00
|
|
|
}
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func (opt *Options) getNetwork() string {
|
|
|
|
if opt.Network == "" {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
return opt.Network
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2012-07-26 19:16:17 +04:00
|
|
|
|
2015-05-02 16:11:18 +03:00
|
|
|
func (opt *Options) getDialer() func() (net.Conn, error) {
|
|
|
|
if opt.Dialer == nil {
|
2015-05-23 17:55:08 +03:00
|
|
|
opt.Dialer = func() (net.Conn, error) {
|
2015-05-02 16:11:18 +03:00
|
|
|
return net.DialTimeout(opt.getNetwork(), opt.Addr, opt.getDialTimeout())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return opt.Dialer
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (opt *Options) getPoolSize() int {
|
|
|
|
if opt.PoolSize == 0 {
|
|
|
|
return 10
|
|
|
|
}
|
|
|
|
return opt.PoolSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opt *Options) getDialTimeout() time.Duration {
|
|
|
|
if opt.DialTimeout == 0 {
|
|
|
|
return 5 * time.Second
|
|
|
|
}
|
|
|
|
return opt.DialTimeout
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2012-08-05 22:09:38 +04:00
|
|
|
|
2015-01-31 16:20:37 +03:00
|
|
|
func (opt *Options) getPoolTimeout() time.Duration {
|
|
|
|
if opt.PoolTimeout == 0 {
|
2015-04-17 14:44:56 +03:00
|
|
|
return 1 * time.Second
|
2015-01-31 16:20:37 +03:00
|
|
|
}
|
|
|
|
return opt.PoolTimeout
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func (opt *Options) getIdleTimeout() time.Duration {
|
|
|
|
return opt.IdleTimeout
|
2014-05-11 18:11:55 +04:00
|
|
|
}
|
2012-08-05 22:09:38 +04:00
|
|
|
|
2015-01-24 15:12:48 +03: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 {
|
2014-05-11 11:42:40 +04:00
|
|
|
*baseClient
|
2015-01-24 15:12:48 +03:00
|
|
|
commandable
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:55:08 +03:00
|
|
|
func newClient(opt *Options, pool pool) *Client {
|
2015-01-24 15:12:48 +03:00
|
|
|
base := &baseClient{opt: opt, connPool: pool}
|
|
|
|
return &Client{
|
|
|
|
baseClient: base,
|
|
|
|
commandable: commandable{process: base.process},
|
|
|
|
}
|
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 {
|
|
|
|
pool := newConnPool(opt)
|
2015-05-02 16:11:18 +03:00
|
|
|
return newClient(opt, pool)
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2016-01-19 19:36:40 +03:00
|
|
|
|
|
|
|
// PoolStats returns connection pool stats
|
|
|
|
func (c *Client) PoolStats() *PoolStats {
|
2016-01-25 16:57:09 +03:00
|
|
|
return c.connPool.Stats()
|
2016-01-19 19:36:40 +03:00
|
|
|
}
|