forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
102 lines (85 loc) · 1.68 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package redis
// Not thread-safe.
type Pipeline struct {
commandable
cmds []Cmder
client *baseClient
closed bool
}
func (c *Client) Pipeline() *Pipeline {
pipe := &Pipeline{
client: &baseClient{
opt: c.opt,
connPool: c.connPool,
},
cmds: make([]Cmder, 0, 10),
}
pipe.commandable.process = pipe.process
return pipe
}
func (c *Client) Pipelined(f func(*Pipeline) error) ([]Cmder, error) {
pc := c.Pipeline()
if err := f(pc); err != nil {
return nil, err
}
cmds, err := pc.Exec()
pc.Close()
return cmds, err
}
func (c *Pipeline) process(cmd Cmder) {
c.cmds = append(c.cmds, cmd)
}
func (c *Pipeline) Close() error {
c.closed = true
return nil
}
func (c *Pipeline) Discard() error {
if c.closed {
return errClosed
}
c.cmds = c.cmds[:0]
return nil
}
// Exec always returns list of commands and error of the first failed
// command if any.
func (c *Pipeline) Exec() (cmds []Cmder, retErr error) {
if c.closed {
return nil, errClosed
}
if len(c.cmds) == 0 {
return c.cmds, nil
}
cmds = c.cmds
c.cmds = make([]Cmder, 0, 0)
for i := 0; i <= c.client.opt.MaxRetries; i++ {
if i > 0 {
resetCmds(cmds)
}
cn, err := c.client.conn()
if err != nil {
setCmdsErr(cmds, err)
return cmds, err
}
retErr = c.execCmds(cn, cmds)
c.client.putConn(cn, err)
if shouldRetry(err) {
continue
}
break
}
return cmds, retErr
}
func (c *Pipeline) execCmds(cn *conn, cmds []Cmder) error {
if err := cn.writeCmds(cmds...); err != nil {
setCmdsErr(cmds, err)
return err
}
var firstCmdErr error
for _, cmd := range cmds {
err := cmd.parseReply(cn.rd)
if err != nil && firstCmdErr == nil {
firstCmdErr = err
}
}
return firstCmdErr
}