redis/redis.go

285 lines
5.0 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-26 19:16:17 +04:00
"fmt"
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
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-06 18:04:30 +04:00
ConnPool: NewSingleConnPool(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
}
//------------------------------------------------------------------------------
func (c *Client) Discard() {
2012-07-25 17:00:50 +04:00
c.mtx.Lock()
2012-07-26 19:16:17 +04:00
c.reqs = c.reqs[:0]
2012-07-25 17:00:50 +04:00
c.mtx.Unlock()
}
2012-07-26 19:16:17 +04:00
func (c *Client) Exec() ([]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-26 19:16:17 +04:00
reqs := c.reqs
c.reqs = make([]Req, 0)
c.mtx.Unlock()
conn, err := c.conn()
if err != nil {
return nil, err
}
err = c.ExecReqs(reqs, conn)
if err != nil {
c.ConnPool.Remove(conn)
return nil, err
}
c.ConnPool.Add(conn)
return reqs, nil
}
func (c *Client) ExecReqs(reqs []Req, conn *Conn) error {
2012-07-26 19:16:17 +04:00
multiReq := make([]byte, 0, 1024)
multiReq = append(multiReq, PackReq([]string{"MULTI"})...)
for _, req := range reqs {
multiReq = append(multiReq, req.Req()...)
}
multiReq = append(multiReq, PackReq([]string{"EXEC"})...)
2012-08-09 14:12:41 +04:00
err := c.WriteReq(multiReq, conn)
2012-07-25 17:00:50 +04:00
if err != nil {
return err
2012-07-25 17:00:50 +04:00
}
2012-07-26 19:16:17 +04:00
statusReq := NewStatusReq()
2012-07-29 13:42:00 +04:00
// Parse MULTI command reply.
2012-08-05 16:09:43 +04:00
_, err = statusReq.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
2012-07-29 13:42:00 +04:00
// Parse queued replies.
2012-07-26 19:16:17 +04:00
for _ = range reqs {
2012-08-05 16:09:43 +04:00
_, err = statusReq.ParseReply(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
}
2012-07-29 13:42:00 +04:00
// Parse number of replies.
2012-08-09 14:12:41 +04:00
line, err := readLine(conn.Rd)
2012-07-26 19:16:17 +04:00
if err != nil {
return err
2012-07-26 19:16:17 +04:00
}
if line[0] != '*' {
2012-08-09 14:12:41 +04:00
buf, _ := conn.Rd.Peek(conn.Rd.Buffered())
return fmt.Errorf("Expected '*', but got line %q of %q.", line, buf)
2012-07-26 19:16:17 +04:00
}
2012-07-29 13:42:00 +04:00
// Parse replies.
2012-08-05 16:09:43 +04:00
for i := 0; i < len(reqs); i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
2012-07-26 22:43:21 +04:00
if err != nil {
req.SetErr(err)
2012-07-29 13:42:00 +04:00
} else {
req.SetVal(val)
2012-07-26 22:43:21 +04:00
}
2012-07-26 19:16:17 +04:00
}
return nil
2012-07-25 17:00:50 +04:00
}