redis/v2/redis.go

202 lines
3.6 KiB
Go
Raw Normal View History

2013-07-02 15:17:31 +04:00
package redis
import (
"log"
"net"
"os"
"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
2013-09-29 12:06:49 +04:00
cmds []Cmder
2013-07-02 15:17:31 +04:00
}
2013-09-29 12:06:49 +04:00
func (c *baseClient) writeCmd(cn *conn, cmds ...Cmder) error {
2013-07-02 15:17:31 +04:00
buf := make([]byte, 0, 1000)
2013-09-29 12:06:49 +04:00
for _, cmd := range cmds {
buf = appendCmd(buf, cmd.args())
2013-07-02 15:17:31 +04:00
}
_, 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{
2013-09-18 18:00:33 +04:00
opt: c.opt,
2013-07-02 15:17:31 +04:00
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
}
2013-09-29 11:17:39 +04:00
func (c *baseClient) freeConn(cn *conn, err error) {
if err == Nil {
c.putConn(cn)
} else {
c.removeConn(cn)
}
}
2013-07-02 15:17:31 +04:00
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)
}
}
2013-09-29 12:06:49 +04:00
func (c *baseClient) Process(cmd Cmder) {
if c.cmds == nil {
c.run(cmd)
2013-07-02 15:17:31 +04:00
} else {
2013-09-29 12:06:49 +04:00
c.cmds = append(c.cmds, cmd)
2013-07-02 15:17:31 +04:00
}
}
2013-09-29 12:06:49 +04:00
func (c *baseClient) run(cmd Cmder) {
2013-07-02 15:17:31 +04:00
cn, err := c.conn()
if err != nil {
2013-09-29 12:06:49 +04:00
cmd.setErr(err)
2013-07-02 15:17:31 +04:00
return
}
2013-09-11 20:22:10 +04:00
cn.writeTimeout = c.opt.WriteTimeout
2013-09-29 12:06:49 +04:00
if timeout := cmd.writeTimeout(); timeout != nil {
2013-09-11 20:22:10 +04:00
cn.writeTimeout = *timeout
}
cn.readTimeout = c.opt.ReadTimeout
2013-09-29 12:06:49 +04:00
if timeout := cmd.readTimeout(); timeout != nil {
2013-09-11 20:22:10 +04:00
cn.readTimeout = *timeout
}
2013-09-29 12:06:49 +04:00
if err := c.writeCmd(cn, cmd); err != nil {
2013-07-02 15:17:31 +04:00
c.removeConn(cn)
2013-09-29 12:06:49 +04:00
cmd.setErr(err)
2013-07-02 15:17:31 +04:00
return
}
2013-09-29 12:11:18 +04:00
val, err := cmd.parseReply(cn.rd)
2013-07-02 15:17:31 +04:00
if err != nil {
2013-09-29 11:17:39 +04:00
c.freeConn(cn, err)
2013-09-29 12:06:49 +04:00
cmd.setErr(err)
2013-07-02 15:17:31 +04:00
return
}
c.putConn(cn)
2013-09-29 12:06:49 +04:00
cmd.setVal(val)
2013-07-02 15:17:31 +04:00
}
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-29 12:15:18 +04:00
func NewTCPClient(opt *Options) *Client {
2013-09-11 19:06:36 +04:00
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-29 12:15:18 +04:00
func NewUnixClient(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
}