redis/redis.go

252 lines
5.2 KiB
Go
Raw Normal View History

2015-01-17 13:07:35 +03:00
package redis // import "gopkg.in/redis.v2"
2012-07-25 17:00:50 +04:00
import (
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
2014-05-11 18:11:55 +04:00
opt *options
2012-07-26 22:43:21 +04:00
}
2014-05-11 11:42:40 +04:00
func (c *baseClient) conn() (*conn, error) {
cn, isNew, err := c.connPool.Get()
if err != nil {
return nil, err
}
2014-06-28 15:47:37 +04:00
if isNew {
if err := c.initConn(cn); err != nil {
2014-05-11 11:42:40 +04:00
c.removeConn(cn)
2012-10-26 19:21:14 +04:00
return nil, err
}
2012-08-05 16:09:43 +04:00
}
2012-07-25 17:00:50 +04:00
2014-05-11 11:42:40 +04:00
return cn, nil
}
2014-06-28 15:47:37 +04:00
func (c *baseClient) initConn(cn *conn) error {
2014-07-08 12:24:19 +04:00
if c.opt.Password == "" && c.opt.DB == 0 {
2014-06-28 15:47:37 +04:00
return nil
}
pool := newSingleConnPool(c.connPool, false)
pool.SetConn(cn)
// Client is not closed because we want to reuse underlying connection.
2015-01-24 15:12:48 +03:00
client := newClient(c.opt, pool)
2014-06-28 15:47:37 +04:00
if c.opt.Password != "" {
if err := client.Auth(c.opt.Password).Err(); err != nil {
return err
2012-08-05 16:09:43 +04:00
}
2014-05-11 11:42:40 +04:00
}
2012-07-29 13:42:00 +04:00
2014-06-28 15:47:37 +04:00
if c.opt.DB > 0 {
if err := client.Select(c.opt.DB).Err(); err != nil {
return err
2012-08-05 16:09:43 +04:00
}
2012-07-25 17:00:50 +04:00
}
2014-05-11 11:42:40 +04:00
return nil
2012-07-26 19:16:17 +04:00
}
func (c *baseClient) freeConn(cn *conn, ei error) error {
if cn.rd.Buffered() > 0 {
return c.connPool.Remove(cn)
} else if ei == nil {
return c.connPool.Put(cn)
} else if _, ok := ei.(redisError); ok {
return c.connPool.Put(cn)
2012-08-14 19:20:22 +04:00
}
return c.connPool.Remove(cn)
2012-08-09 18:06:26 +04:00
}
2014-05-11 11:42:40 +04:00
func (c *baseClient) removeConn(cn *conn) {
if err := c.connPool.Remove(cn); err != nil {
2014-07-31 17:01:54 +04:00
log.Printf("pool.Remove failed: %s", err)
}
2014-05-11 11:42:40 +04:00
}
2014-05-11 11:42:40 +04:00
func (c *baseClient) putConn(cn *conn) {
if err := c.connPool.Put(cn); err != nil {
2014-07-31 17:01:54 +04:00
log.Printf("pool.Put failed: %s", err)
}
}
2015-01-24 15:12:48 +03:00
func (c *baseClient) process(cmd Cmder) {
2014-05-11 11:42:40 +04:00
cn, err := c.conn()
2012-07-26 22:43:21 +04:00
if err != nil {
2014-05-11 11:42:40 +04:00
cmd.setErr(err)
2012-07-26 22:43:21 +04:00
return
}
2014-05-11 11:42:40 +04:00
if timeout := cmd.writeTimeout(); timeout != nil {
cn.writeTimeout = *timeout
2014-06-28 15:47:37 +04:00
} else {
cn.writeTimeout = c.opt.WriteTimeout
2012-07-26 22:43:21 +04:00
}
2014-05-11 11:42:40 +04:00
if timeout := cmd.readTimeout(); timeout != nil {
cn.readTimeout = *timeout
2014-06-28 15:47:37 +04:00
} else {
cn.readTimeout = c.opt.ReadTimeout
2014-05-11 11:42:40 +04:00
}
2015-01-24 15:12:48 +03:00
if err := cn.writeCmds(cmd); err != nil {
2014-05-11 11:42:40 +04:00
c.freeConn(cn, err)
cmd.setErr(err)
2012-07-26 19:16:17 +04:00
return
}
2012-08-05 16:09:43 +04:00
2014-05-11 11:42:40 +04:00
if err := cmd.parseReply(cn.rd); err != nil {
c.freeConn(cn, err)
return
}
2014-05-11 11:42:40 +04:00
c.putConn(cn)
}
// Close closes the client, releasing any open resources.
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
//------------------------------------------------------------------------------
2014-05-11 18:11:55 +04:00
type options struct {
Password string
DB int64
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolSize int
2015-01-31 16:20:37 +03:00
PoolTimeout time.Duration
2014-05-11 18:11:55 +04:00
IdleTimeout time.Duration
}
2014-05-11 11:42:40 +04:00
type Options struct {
2015-01-31 17:54:37 +03:00
// The network type, either "tcp" or "unix".
// Default: "tcp"
2014-09-30 12:46:56 +04:00
Network string
2015-01-31 17:54:37 +03:00
// The network address.
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
// `requirepass` server configuration option.
2014-05-11 11:42:40 +04:00
Password string
2015-01-31 17:54:37 +03:00
// Select a database.
// Default: 0
DB int64
// Sets the deadline for establishing new connections. If reached,
// deal attepts will fail with a timeout.
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.
// Default: 10
PoolSize int
// PoolTimeout specifies amount of time client waits for a free
// connection in the pool. Default timeout is 1s.
2015-01-31 16:20:37 +03:00
PoolTimeout time.Duration
2015-01-31 17:54:37 +03:00
// Evict connections from the pool after they have been idle for longer
// than specified in this option.
// Default: 0 = no eviction
2014-05-11 18:11:55 +04:00
IdleTimeout time.Duration
}
2012-07-26 19:16:17 +04:00
2015-01-30 17:45:57 +03:00
func (opt *Options) getNetwork() string {
if opt.Network == "" {
return "tcp"
}
return opt.Network
}
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
}
2015-01-31 16:20:37 +03:00
func (opt *Options) getPoolTimeout() time.Duration {
if opt.PoolTimeout == 0 {
return 1 * time.Second
2015-01-31 16:20:37 +03:00
}
return opt.PoolTimeout
}
2014-05-11 18:11:55 +04:00
func (opt *Options) options() *options {
return &options{
DB: opt.DB,
Password: opt.Password,
DialTimeout: opt.getDialTimeout(),
ReadTimeout: opt.ReadTimeout,
WriteTimeout: opt.WriteTimeout,
PoolSize: opt.getPoolSize(),
2015-01-31 16:20:37 +03:00
PoolTimeout: opt.getPoolTimeout(),
2014-05-11 18:11:55 +04:00
IdleTimeout: opt.IdleTimeout,
}
}
2015-01-24 15:12:48 +03:00
//------------------------------------------------------------------------------
type Client struct {
2014-05-11 11:42:40 +04:00
*baseClient
2015-01-24 15:12:48 +03:00
commandable
}
func newClient(opt *options, pool pool) *Client {
base := &baseClient{opt: opt, connPool: pool}
return &Client{
baseClient: base,
commandable: commandable{process: base.process},
}
2012-08-05 16:09:43 +04:00
}
2014-09-30 12:46:56 +04:00
func NewClient(clOpt *Options) *Client {
2014-05-11 18:11:55 +04:00
opt := clOpt.options()
2014-09-30 12:46:56 +04:00
dialer := clOpt.Dialer
if dialer == nil {
dialer = func() (net.Conn, error) {
2015-01-30 17:45:57 +03:00
return net.DialTimeout(clOpt.getNetwork(), clOpt.Addr, opt.DialTimeout)
2014-09-30 12:46:56 +04:00
}
2014-05-11 18:11:55 +04:00
}
2015-01-24 15:12:48 +03:00
return newClient(opt, newConnPool(newConnFunc(dialer), opt))
}
2012-08-05 16:09:43 +04:00
2014-09-30 12:46:56 +04:00
// Deprecated. Use NewClient instead.
2014-05-11 11:42:40 +04:00
func NewTCPClient(opt *Options) *Client {
2014-09-30 12:46:56 +04:00
opt.Network = "tcp"
return NewClient(opt)
2012-07-29 13:42:00 +04:00
}
2014-09-30 12:46:56 +04:00
// Deprecated. Use NewClient instead.
2014-05-11 11:42:40 +04:00
func NewUnixClient(opt *Options) *Client {
2014-09-30 12:46:56 +04:00
opt.Network = "unix"
return NewClient(opt)
}