2012-08-11 18:42:10 +04:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2012-08-20 13:10:00 +04:00
|
|
|
"sync"
|
2012-08-11 18:42:10 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type MultiClient struct {
|
|
|
|
*Client
|
2012-08-20 13:10:00 +04:00
|
|
|
execMtx sync.Mutex
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) MultiClient() (*MultiClient, error) {
|
|
|
|
return &MultiClient{
|
|
|
|
Client: &Client{
|
|
|
|
BaseClient: &BaseClient{
|
|
|
|
ConnPool: NewSingleConnPool(c.ConnPool, true),
|
|
|
|
InitConn: c.InitConn,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2012-08-20 00:59:52 +04:00
|
|
|
func (c *MultiClient) Close() error {
|
|
|
|
c.Unwatch()
|
|
|
|
return c.Client.Close()
|
|
|
|
}
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
func (c *MultiClient) Watch(keys ...string) *StatusReq {
|
|
|
|
args := append([]string{"WATCH"}, keys...)
|
|
|
|
req := NewStatusReq(args...)
|
|
|
|
c.Process(req)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MultiClient) Unwatch(keys ...string) *StatusReq {
|
|
|
|
args := append([]string{"UNWATCH"}, keys...)
|
|
|
|
req := NewStatusReq(args...)
|
|
|
|
c.Process(req)
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MultiClient) Discard() {
|
|
|
|
c.mtx.Lock()
|
2012-08-14 19:20:22 +04:00
|
|
|
c.reqs = []Req{NewStatusReq("MULTI")}
|
2012-08-11 18:42:10 +04:00
|
|
|
c.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2012-08-13 15:45:32 +04:00
|
|
|
func (c *MultiClient) Exec(do func()) ([]Req, error) {
|
2012-08-14 19:20:22 +04:00
|
|
|
c.Discard()
|
2012-08-13 15:45:32 +04:00
|
|
|
|
|
|
|
do()
|
|
|
|
|
2012-08-11 18:42:10 +04:00
|
|
|
c.mtx.Lock()
|
2012-08-17 22:36:48 +04:00
|
|
|
c.reqs = append(c.reqs, NewIfaceSliceReq("EXEC"))
|
2012-08-14 19:20:22 +04:00
|
|
|
if len(c.reqs) == 2 {
|
2012-08-11 18:42:10 +04:00
|
|
|
c.mtx.Unlock()
|
2012-08-14 19:20:22 +04:00
|
|
|
return []Req{}, nil
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
reqs := c.reqs
|
|
|
|
c.reqs = nil
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
conn, err := c.conn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-08-20 13:10:00 +04:00
|
|
|
// Synchronize writes and reads to the connection using mutex.
|
|
|
|
c.execMtx.Lock()
|
2012-08-11 18:42:10 +04:00
|
|
|
err = c.ExecReqs(reqs, conn)
|
2012-08-20 13:10:00 +04:00
|
|
|
c.execMtx.Unlock()
|
2012-08-11 18:42:10 +04:00
|
|
|
if err != nil {
|
|
|
|
c.ConnPool.Remove(conn)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ConnPool.Add(conn)
|
2012-08-14 19:20:22 +04:00
|
|
|
return reqs[1 : len(reqs)-1], nil
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MultiClient) ExecReqs(reqs []Req, conn *Conn) error {
|
2012-08-14 19:20:22 +04:00
|
|
|
err := c.WriteReq(conn, reqs...)
|
2012-08-11 18:42:10 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
statusReq := NewStatusReq()
|
|
|
|
|
2012-08-20 12:40:27 +04:00
|
|
|
// Omit last request (EXEC).
|
|
|
|
reqsLen := len(reqs) - 1
|
2012-08-11 18:42:10 +04:00
|
|
|
|
|
|
|
// Parse queued replies.
|
2012-08-20 12:40:27 +04:00
|
|
|
for i := 0; i < reqsLen; i++ {
|
2012-08-11 18:42:10 +04:00
|
|
|
_, err = statusReq.ParseReply(conn.Rd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse number of replies.
|
|
|
|
line, err := readLine(conn.Rd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if line[0] != '*' {
|
|
|
|
return fmt.Errorf("Expected '*', but got line %q", line)
|
|
|
|
}
|
2012-08-12 22:41:44 +04:00
|
|
|
if len(line) == 3 && line[1] == '-' && line[2] == '1' {
|
2012-08-11 18:42:10 +04:00
|
|
|
return Nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse replies.
|
2012-08-20 12:40:27 +04:00
|
|
|
// Loop starts from 1 to omit first request (MULTI).
|
|
|
|
for i := 1; i < reqsLen; i++ {
|
2012-08-11 18:42:10 +04:00
|
|
|
req := reqs[i]
|
|
|
|
val, err := req.ParseReply(conn.Rd)
|
|
|
|
if err != nil {
|
|
|
|
req.SetErr(err)
|
|
|
|
} else {
|
|
|
|
req.SetVal(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|