redis/redis.go

162 lines
2.9 KiB
Go
Raw Normal View History

2012-07-25 17:00:50 +04:00
package redis
import (
2012-08-05 16:09:43 +04:00
"crypto/tls"
2012-07-25 17:00:50 +04:00
"io"
2012-08-05 16:09:43 +04:00
"net"
2012-07-25 17:00:50 +04:00
"sync"
2012-08-09 14:12:41 +04:00
)
2012-07-25 17:00:50 +04:00
type OpenConnFunc func() (io.ReadWriteCloser, error)
type CloseConnFunc func(io.ReadWriteCloser) error
2012-08-05 16:09:43 +04:00
type InitConnFunc func(*Client) error
2012-07-25 17:00:50 +04:00
2012-08-05 16:09:43 +04:00
func TCPConnector(addr string) OpenConnFunc {
return func() (io.ReadWriteCloser, error) {
2012-08-05 16:09:43 +04:00
return net.Dial("tcp", addr)
}
2012-07-26 22:43:21 +04:00
}
2012-08-05 16:09:43 +04:00
func TLSConnector(addr string, tlsConfig *tls.Config) OpenConnFunc {
return func() (io.ReadWriteCloser, error) {
2012-08-05 16:09:43 +04:00
return tls.Dial("tcp", addr, tlsConfig)
}
2012-07-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +04:00
func AuthSelectFunc(password string, db int64) InitConnFunc {
if password == "" && db < 0 {
return nil
}
2012-08-05 16:09:43 +04:00
return func(client *Client) error {
if password != "" {
2012-08-06 16:09:48 +04:00
auth := client.Auth(password)
if auth.Err() != nil {
return auth.Err()
2012-08-05 16:09:43 +04:00
}
}
2012-07-29 13:42:00 +04:00
if db >= 0 {
2012-08-06 16:09:48 +04:00
sel := client.Select(db)
if sel.Err() != nil {
return sel.Err()
}
2012-08-05 16:09:43 +04:00
}
return nil
2012-07-25 17:00:50 +04:00
}
}
2012-08-09 14:12:41 +04:00
//------------------------------------------------------------------------------
2012-07-26 19:16:17 +04:00
type BaseClient struct {
2012-08-05 16:09:43 +04:00
mtx sync.Mutex
ConnPool ConnPool
2012-08-05 16:09:43 +04:00
InitConn InitConnFunc
reqs []Req
2012-07-26 19:16:17 +04:00
}
2012-08-14 19:20:22 +04:00
func (c *BaseClient) WriteReq(conn *Conn, reqs ...Req) error {
2012-08-17 22:36:48 +04:00
buf := make([]byte, 0, 1000)
2012-08-14 19:20:22 +04:00
for _, req := range reqs {
2012-08-17 22:36:48 +04:00
buf = appendReq(buf, req.Args())
2012-08-14 19:20:22 +04:00
}
2012-08-17 22:36:48 +04:00
_, err := conn.RW.Write(buf)
return err
2012-08-09 18:06:26 +04:00
}
func (c *BaseClient) conn() (*Conn, error) {
conn, isNew, err := c.ConnPool.Get()
if err != nil {
return nil, err
}
if isNew && c.InitConn != nil {
client := &Client{
BaseClient: &BaseClient{
ConnPool: NewSingleConnPoolConn(c.ConnPool, conn, true),
},
}
err = c.InitConn(client)
if err != nil {
return nil, err
}
}
return conn, nil
}
func (c *BaseClient) Process(req Req) {
2012-08-05 16:09:43 +04:00
if c.reqs == nil {
c.Run(req)
} else {
c.Queue(req)
}
2012-07-25 17:00:50 +04:00
}
func (c *BaseClient) Run(req Req) {
conn, err := c.conn()
2012-07-26 22:43:21 +04:00
if err != nil {
req.SetErr(err)
return
}
2012-08-14 19:20:22 +04:00
err = c.WriteReq(conn, req)
2012-07-26 22:43:21 +04:00
if err != nil {
2012-08-05 16:09:43 +04:00
c.ConnPool.Remove(conn)
2012-07-26 22:43:21 +04:00
req.SetErr(err)
return
}
2012-08-05 16:09:43 +04:00
val, err := req.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
if err == Nil {
c.ConnPool.Add(conn)
} else {
c.ConnPool.Remove(conn)
}
2012-07-26 19:16:17 +04:00
req.SetErr(err)
return
}
2012-08-05 16:09:43 +04:00
c.ConnPool.Add(conn)
2012-07-26 22:43:21 +04:00
req.SetVal(val)
2012-07-26 19:16:17 +04:00
}
func (c *BaseClient) Queue(req Req) {
2012-08-05 16:09:43 +04:00
c.mtx.Lock()
c.reqs = append(c.reqs, req)
2012-07-29 13:42:00 +04:00
c.mtx.Unlock()
}
2012-07-26 19:16:17 +04:00
func (c *BaseClient) Close() error {
return c.ConnPool.Close()
}
//------------------------------------------------------------------------------
type Client struct {
*BaseClient
2012-08-05 16:09:43 +04:00
}
func NewClient(openConn OpenConnFunc, closeConn CloseConnFunc, initConn InitConnFunc) *Client {
return &Client{
BaseClient: &BaseClient{
ConnPool: NewMultiConnPool(openConn, closeConn, 10),
InitConn: initConn,
},
2012-07-29 13:42:00 +04:00
}
}
2012-08-05 16:09:43 +04:00
func NewTCPClient(addr string, password string, db int64) *Client {
return NewClient(TCPConnector(addr), nil, AuthSelectFunc(password, db))
}
2012-07-29 13:42:00 +04:00
func NewTLSClient(addr string, tlsConfig *tls.Config, password string, db int64) *Client {
return NewClient(
TLSConnector(addr, tlsConfig),
nil,
AuthSelectFunc(password, db),
)
2012-07-29 13:42:00 +04:00
}