2013-07-02 15:17:31 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Package logger.
|
|
|
|
var Logger = log.New(os.Stdout, "redis: ", log.Ldate|log.Ltime)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type baseClient struct {
|
|
|
|
connPool pool
|
|
|
|
|
2013-09-11 20:22:10 +04:00
|
|
|
opt *Options
|
2013-07-02 15:17:31 +04:00
|
|
|
|
|
|
|
reqs []Req
|
|
|
|
reqsMtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) writeReq(cn *conn, reqs ...Req) error {
|
|
|
|
buf := make([]byte, 0, 1000)
|
|
|
|
for _, req := range reqs {
|
|
|
|
buf = appendReq(buf, req.Args())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := cn.Write(buf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) conn() (*conn, error) {
|
|
|
|
cn, isNew, err := c.connPool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:22:10 +04:00
|
|
|
if isNew && (c.opt.Password != "" || c.opt.DB > 0) {
|
|
|
|
if err = c.init(cn, c.opt.Password, c.opt.DB); err != nil {
|
2013-07-02 15:17:31 +04:00
|
|
|
c.removeConn(cn)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) init(cn *conn, password string, db int64) error {
|
|
|
|
// Client is not closed on purpose.
|
|
|
|
client := &Client{
|
|
|
|
baseClient: &baseClient{
|
|
|
|
connPool: newSingleConnPool(c.connPool, cn, false),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if password != "" {
|
|
|
|
auth := client.Auth(password)
|
|
|
|
if auth.Err() != nil {
|
|
|
|
return auth.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if db > 0 {
|
|
|
|
sel := client.Select(db)
|
|
|
|
if sel.Err() != nil {
|
|
|
|
return sel.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) removeConn(cn *conn) {
|
|
|
|
if err := c.connPool.Remove(cn); err != nil {
|
|
|
|
Logger.Printf("connPool.Remove error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) putConn(cn *conn) {
|
|
|
|
if err := c.connPool.Put(cn); err != nil {
|
|
|
|
Logger.Printf("connPool.Add error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) Process(req Req) {
|
|
|
|
if c.reqs == nil {
|
|
|
|
c.run(req)
|
|
|
|
} else {
|
|
|
|
c.queue(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) run(req Req) {
|
|
|
|
cn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
req.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:22:10 +04:00
|
|
|
cn.writeTimeout = c.opt.WriteTimeout
|
|
|
|
if timeout := req.writeTimeout(); timeout != nil {
|
|
|
|
cn.writeTimeout = *timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
cn.readTimeout = c.opt.ReadTimeout
|
|
|
|
if timeout := req.readTimeout(); timeout != nil {
|
|
|
|
cn.readTimeout = *timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.writeReq(cn, req); err != nil {
|
2013-07-02 15:17:31 +04:00
|
|
|
c.removeConn(cn)
|
|
|
|
req.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := req.ParseReply(cn.Rd)
|
|
|
|
if err != nil {
|
2013-09-17 11:35:52 +04:00
|
|
|
if err == Nil {
|
2013-07-02 15:17:31 +04:00
|
|
|
c.putConn(cn)
|
2013-09-17 11:35:52 +04:00
|
|
|
} else {
|
|
|
|
c.removeConn(cn)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
req.SetErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.putConn(cn)
|
|
|
|
req.SetVal(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queues request to be executed later.
|
|
|
|
func (c *baseClient) queue(req Req) {
|
|
|
|
c.reqsMtx.Lock()
|
|
|
|
c.reqs = append(c.reqs, req)
|
|
|
|
c.reqsMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *baseClient) Close() error {
|
|
|
|
return c.connPool.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
type Options struct {
|
|
|
|
Addr string
|
2013-07-02 15:17:31 +04:00
|
|
|
Password string
|
|
|
|
DB int64
|
|
|
|
|
|
|
|
PoolSize int
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
DialTimeout time.Duration
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func (opt *Options) getPoolSize() int {
|
|
|
|
if opt.PoolSize == 0 {
|
|
|
|
return 10
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
2013-09-11 19:06:36 +04:00
|
|
|
return opt.PoolSize
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func (opt *Options) getDialTimeout() time.Duration {
|
|
|
|
if opt.DialTimeout == 0 {
|
|
|
|
return 5 * time.Second
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
2013-09-11 19:06:36 +04:00
|
|
|
return opt.DialTimeout
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
*baseClient
|
|
|
|
}
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func newClient(opt *Options, dial func() (net.Conn, error)) *Client {
|
|
|
|
return &Client{
|
|
|
|
baseClient: &baseClient{
|
2013-09-11 20:22:10 +04:00
|
|
|
opt: opt,
|
2013-09-11 19:06:36 +04:00
|
|
|
|
|
|
|
connPool: newConnPool(
|
|
|
|
dial, opt.getPoolSize(),
|
2013-09-11 20:22:10 +04:00
|
|
|
opt.IdleTimeout,
|
2013-09-11 19:06:36 +04:00
|
|
|
),
|
|
|
|
},
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
2013-09-11 19:06:36 +04:00
|
|
|
}
|
2013-07-02 15:17:31 +04:00
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func DialTCP(opt *Options) *Client {
|
|
|
|
dial := func() (net.Conn, error) {
|
|
|
|
return net.DialTimeout("tcp", opt.Addr, opt.getDialTimeout())
|
|
|
|
}
|
|
|
|
return newClient(opt, dial)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func DialTLS(opt *Options, tlsConfig *tls.Config) *Client {
|
2013-07-02 15:17:31 +04:00
|
|
|
dial := func() (net.Conn, error) {
|
2013-09-11 19:06:36 +04:00
|
|
|
conn, err := net.DialTimeout("tcp", opt.Addr, opt.getDialTimeout())
|
2013-07-02 15:17:31 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tls.Client(conn, tlsConfig), nil
|
|
|
|
}
|
2013-09-11 19:06:36 +04:00
|
|
|
return newClient(opt, dial)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 19:06:36 +04:00
|
|
|
func DialUnix(opt *Options) *Client {
|
2013-07-02 15:17:31 +04:00
|
|
|
dial := func() (net.Conn, error) {
|
2013-09-11 19:06:36 +04:00
|
|
|
return net.DialTimeout("unix", opt.Addr, opt.getDialTimeout())
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|
2013-09-11 19:06:36 +04:00
|
|
|
return newClient(opt, dial)
|
2013-07-02 15:17:31 +04:00
|
|
|
}
|