redis/redis.go

202 lines
3.5 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-08-09 14:12:41 +04:00
"errors"
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
2012-08-09 14:12:41 +04:00
var (
ErrReaderTooSmall = errors.New("redis: Reader is too small")
2012-07-25 17:00:50 +04:00
)
2012-08-05 16:09:43 +04:00
type OpenConnFunc func() (io.ReadWriter, error)
type CloseConnFunc func(io.ReadWriter)
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.ReadWriter, error) {
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.ReadWriter, error) {
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
2012-08-05 16:09:43 +04:00
type Client struct {
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-05 16:09:43 +04:00
func NewClient(openConn OpenConnFunc, closeConn CloseConnFunc, initConn InitConnFunc) *Client {
return &Client{
ConnPool: NewMultiConnPool(openConn, closeConn, 10),
2012-08-05 16:09:43 +04:00
InitConn: initConn,
2012-07-25 17:00:50 +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-25 17:00:50 +04:00
}
2012-08-05 16:09:43 +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-26 22:43:21 +04:00
2012-08-09 18:06:26 +04:00
func (c *Client) Close() {
c.ConnPool.Close()
}
func (c *Client) conn() (*Conn, error) {
conn, isNew, err := c.ConnPool.Get()
if err != nil {
return nil, err
}
if isNew && c.InitConn != nil {
client := &Client{
2012-08-09 18:06:26 +04:00
ConnPool: NewSingleConnPoolConn(c.ConnPool, conn),
}
err = c.InitConn(client)
if err != nil {
return nil, err
}
}
return conn, nil
}
2012-08-05 16:09:43 +04:00
func (c *Client) WriteReq(buf []byte, conn *Conn) error {
_, err := conn.RW.Write(buf)
2012-07-25 17:00:50 +04:00
return err
}
2012-08-05 16:09:43 +04:00
func (c *Client) Process(req Req) {
if c.reqs == nil {
c.Run(req)
} else {
c.Queue(req)
}
2012-07-25 17:00:50 +04:00
}
2012-07-29 13:42:00 +04:00
func (c *Client) Queue(req Req) {
c.mtx.Lock()
c.reqs = append(c.reqs, req)
c.mtx.Unlock()
}
2012-07-26 19:16:17 +04:00
2012-07-29 13:42:00 +04:00
func (c *Client) Run(req Req) {
conn, err := c.conn()
2012-07-26 22:43:21 +04:00
if err != nil {
req.SetErr(err)
return
}
2012-08-09 14:12:41 +04:00
err = c.WriteReq(req.Req(), conn)
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 {
c.ConnPool.Add(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
}
2012-07-29 13:42:00 +04:00
func (c *Client) RunQueued() ([]Req, error) {
2012-08-05 16:09:43 +04:00
c.mtx.Lock()
2012-07-29 13:51:29 +04:00
if len(c.reqs) == 0 {
2012-08-05 16:09:43 +04:00
c.mtx.Unlock()
2012-07-29 13:51:29 +04:00
return c.reqs, nil
}
2012-07-29 13:42:00 +04:00
reqs := c.reqs
c.reqs = make([]Req, 0)
c.mtx.Unlock()
2012-07-26 19:16:17 +04:00
conn, err := c.conn()
if err != nil {
return nil, err
}
err = c.RunReqs(reqs, conn)
if err != nil {
c.ConnPool.Remove(conn)
return nil, err
}
2012-08-06 12:00:29 +04:00
c.ConnPool.Add(conn)
return reqs, nil
2012-08-05 16:09:43 +04:00
}
func (c *Client) RunReqs(reqs []Req, conn *Conn) error {
2012-07-29 13:42:00 +04:00
var multiReq []byte
if len(reqs) == 1 {
multiReq = reqs[0].Req()
} else {
multiReq = make([]byte, 0, 1024)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
2012-07-26 19:16:17 +04:00
}
2012-08-09 14:12:41 +04:00
err := c.WriteReq(multiReq, conn)
2012-07-29 13:42:00 +04:00
if err != nil {
return err
2012-07-29 13:42:00 +04:00
}
2012-08-05 16:09:43 +04:00
for i := 0; i < len(reqs); i++ {
req := reqs[i]
2012-08-05 16:09:43 +04:00
val, err := req.ParseReply(conn.Rd)
2012-07-29 13:42:00 +04:00
if err != nil {
req.SetErr(err)
} else {
req.SetVal(val)
}
}
return nil
2012-07-29 13:42:00 +04:00
}