forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
84 lines (69 loc) · 1.33 KB
/
pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package redis
type PipelineClient struct {
*Client
}
func (c *Client) PipelineClient() (*PipelineClient, error) {
return &PipelineClient{
Client: &Client{
BaseClient: &BaseClient{
ConnPool: c.ConnPool,
InitConn: c.InitConn,
reqs: make([]Req, 0),
},
},
}, nil
}
func (c *Client) Pipelined(do func(*PipelineClient)) ([]Req, error) {
pc, err := c.PipelineClient()
if err != nil {
return nil, err
}
defer pc.Close()
do(pc)
return pc.RunQueued()
}
func (c *PipelineClient) Close() error {
return nil
}
func (c *PipelineClient) DiscardQueued() {
c.reqsMtx.Lock()
c.reqs = c.reqs[:0]
c.reqsMtx.Unlock()
}
func (c *PipelineClient) RunQueued() ([]Req, error) {
c.reqsMtx.Lock()
reqs := c.reqs
c.reqs = make([]Req, 0)
c.reqsMtx.Unlock()
if len(reqs) == 0 {
return []Req{}, nil
}
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
}
c.ConnPool.Add(conn)
return reqs, nil
}
func (c *PipelineClient) RunReqs(reqs []Req, conn *Conn) error {
err := c.WriteReq(conn, reqs...)
if err != nil {
return err
}
reqsLen := len(reqs)
for i := 0; i < reqsLen; i++ {
req := reqs[i]
val, err := req.ParseReply(conn.Rd)
if err != nil {
req.SetErr(err)
} else {
req.SetVal(val)
}
}
return nil
}