2012-07-25 17:00:50 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2012-08-05 16:09:43 +04:00
|
|
|
"net"
|
2012-10-26 19:21:14 +04:00
|
|
|
"time"
|
2014-05-11 11:42:40 +04:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
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
|
|
|
|
cmds []Cmder
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) writeCmd(cn *conn, cmds ...Cmder) error {
|
|
|
|
buf := make([]byte, 0, 1000)
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
buf = appendCmd(buf, cmd.args())
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
|
|
|
|
_, err := cn.Write(buf)
|
|
|
|
return err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNew && (c.opt.Password != "" || c.opt.DB > 0) {
|
|
|
|
if err = c.init(cn, c.opt.Password, c.opt.DB); err != nil {
|
|
|
|
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
|
2013-02-02 14:50:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) init(cn *conn, password string, db int64) error {
|
|
|
|
// Client is not closed on purpose.
|
|
|
|
client := &Client{
|
|
|
|
baseClient: &baseClient{
|
|
|
|
opt: c.opt,
|
|
|
|
connPool: newSingleConnPool(c.connPool, cn, false),
|
|
|
|
},
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
if password != "" {
|
|
|
|
auth := client.Auth(password)
|
|
|
|
if auth.Err() != nil {
|
|
|
|
return auth.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-05-11 11:42:40 +04:00
|
|
|
if db > 0 {
|
|
|
|
sel := client.Select(db)
|
|
|
|
if sel.Err() != nil {
|
|
|
|
return sel.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
|
|
|
}
|
|
|
|
|
2014-06-18 16:55:49 +04:00
|
|
|
func (c *baseClient) freeConn(cn *conn, ei error) error {
|
|
|
|
if cn.rd.Buffered() > 0 {
|
|
|
|
return c.connPool.Remove(cn)
|
|
|
|
}
|
|
|
|
if _, ok := ei.(redisError); ok {
|
|
|
|
return c.connPool.Put(cn)
|
2012-08-14 19:20:22 +04:00
|
|
|
}
|
2014-06-18 16:55:49 +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 {
|
|
|
|
glog.Errorf("pool.Remove failed: %s", err)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) putConn(cn *conn) {
|
|
|
|
if err := c.connPool.Put(cn); err != nil {
|
|
|
|
glog.Errorf("pool.Put failed: %s", err)
|
2012-08-06 12:33:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) Process(cmd Cmder) {
|
|
|
|
if c.cmds == nil {
|
|
|
|
c.run(cmd)
|
2012-08-05 16:09:43 +04:00
|
|
|
} else {
|
2014-05-11 11:42:40 +04:00
|
|
|
c.cmds = append(c.cmds, cmd)
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
2012-07-25 17:00:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func (c *baseClient) run(cmd Cmder) {
|
|
|
|
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
|
|
|
cn.writeTimeout = c.opt.WriteTimeout
|
|
|
|
if timeout := cmd.writeTimeout(); timeout != nil {
|
|
|
|
cn.writeTimeout = *timeout
|
2012-07-26 22:43:21 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
cn.readTimeout = c.opt.ReadTimeout
|
|
|
|
if timeout := cmd.readTimeout(); timeout != nil {
|
|
|
|
cn.readTimeout = *timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.writeCmd(cn, cmd); err != nil {
|
|
|
|
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
|
2012-08-24 16:16:12 +04:00
|
|
|
}
|
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
|
|
|
|
IdleTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
type Options struct {
|
|
|
|
Addr string
|
|
|
|
Password string
|
|
|
|
DB int64
|
|
|
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
2014-05-11 18:11:55 +04:00
|
|
|
|
|
|
|
PoolSize int
|
|
|
|
IdleTimeout time.Duration
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2012-07-26 19:16:17 +04:00
|
|
|
|
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
|
|
|
|
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(),
|
|
|
|
IdleTimeout: opt.IdleTimeout,
|
|
|
|
}
|
|
|
|
}
|
2012-08-05 22:09:38 +04:00
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
type Client struct {
|
2014-05-11 11:42:40 +04:00
|
|
|
*baseClient
|
2012-08-05 16:09:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 18:11:55 +04:00
|
|
|
func newClient(clOpt *Options, network string) *Client {
|
|
|
|
opt := clOpt.options()
|
|
|
|
dialer := func() (net.Conn, error) {
|
|
|
|
return net.DialTimeout(network, clOpt.Addr, opt.DialTimeout)
|
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
return &Client{
|
2014-05-11 11:42:40 +04:00
|
|
|
baseClient: &baseClient{
|
2014-05-11 18:11:55 +04:00
|
|
|
opt: opt,
|
|
|
|
connPool: newConnPool(newConnFunc(dialer), opt),
|
2012-08-11 18:42:10 +04:00
|
|
|
},
|
2012-07-29 13:42:00 +04:00
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2012-08-05 16:09:43 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func NewTCPClient(opt *Options) *Client {
|
2014-05-11 18:11:55 +04:00
|
|
|
return newClient(opt, "tcp")
|
2012-07-29 13:42:00 +04:00
|
|
|
}
|
2013-02-02 14:50:43 +04:00
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
func NewUnixClient(opt *Options) *Client {
|
2014-05-11 18:11:55 +04:00
|
|
|
return newClient(opt, "unix")
|
2013-02-02 14:50:43 +04:00
|
|
|
}
|