2012-08-11 18:42:10 +04:00
|
|
|
package redis
|
|
|
|
|
2015-11-04 15:25:48 +03:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2016-04-09 13:27:16 +03:00
|
|
|
"gopkg.in/redis.v4/internal/pool"
|
2015-11-04 15:25:48 +03:00
|
|
|
)
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
// Pipeline implements pipelining as described in
|
2015-11-04 15:25:48 +03:00
|
|
|
// http://redis.io/topics/pipelining. It's safe for concurrent use
|
2015-09-12 09:36:03 +03:00
|
|
|
// by multiple goroutines.
|
2014-05-11 11:42:40 +04:00
|
|
|
type Pipeline struct {
|
2015-01-24 15:12:48 +03:00
|
|
|
commandable
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2016-04-09 10:47:15 +03:00
|
|
|
exec func([]Cmder) error
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2015-11-04 15:25:48 +03:00
|
|
|
mu sync.Mutex // protects cmds
|
|
|
|
cmds []Cmder
|
|
|
|
|
|
|
|
closed int32
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
func (pipe *Pipeline) process(cmd Cmder) {
|
2015-11-04 15:25:48 +03:00
|
|
|
pipe.mu.Lock()
|
2015-06-04 11:50:24 +03:00
|
|
|
pipe.cmds = append(pipe.cmds, cmd)
|
2015-11-04 15:25:48 +03:00
|
|
|
pipe.mu.Unlock()
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// Close closes the pipeline, releasing any open resources.
|
2015-06-04 11:50:24 +03:00
|
|
|
func (pipe *Pipeline) Close() error {
|
2015-11-04 15:25:48 +03:00
|
|
|
atomic.StoreInt32(&pipe.closed, 1)
|
2015-06-04 11:50:24 +03:00
|
|
|
pipe.Discard()
|
2012-08-11 18:42:10 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-04 15:25:48 +03:00
|
|
|
func (pipe *Pipeline) isClosed() bool {
|
|
|
|
return atomic.LoadInt32(&pipe.closed) == 1
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
// Discard resets the pipeline and discards queued commands.
|
|
|
|
func (pipe *Pipeline) Discard() error {
|
2015-11-04 15:25:48 +03:00
|
|
|
defer pipe.mu.Unlock()
|
|
|
|
pipe.mu.Lock()
|
|
|
|
if pipe.isClosed() {
|
2016-03-14 14:17:33 +03:00
|
|
|
return pool.ErrClosed
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
pipe.cmds = pipe.cmds[:0]
|
2014-05-11 11:42:40 +04:00
|
|
|
return nil
|
2012-08-20 00:19:00 +04:00
|
|
|
}
|
|
|
|
|
2015-11-04 15:25:48 +03:00
|
|
|
// Exec executes all previously queued commands using one
|
|
|
|
// client-server roundtrip.
|
|
|
|
//
|
2014-05-11 11:42:40 +04:00
|
|
|
// Exec always returns list of commands and error of the first failed
|
|
|
|
// command if any.
|
2016-04-09 10:47:15 +03:00
|
|
|
func (pipe *Pipeline) Exec() ([]Cmder, error) {
|
2015-11-04 15:25:48 +03:00
|
|
|
if pipe.isClosed() {
|
2016-03-14 14:17:33 +03:00
|
|
|
return nil, pool.ErrClosed
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2015-11-04 15:25:48 +03:00
|
|
|
|
|
|
|
defer pipe.mu.Unlock()
|
|
|
|
pipe.mu.Lock()
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
if len(pipe.cmds) == 0 {
|
|
|
|
return pipe.cmds, nil
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2016-04-09 10:47:15 +03:00
|
|
|
cmds := pipe.cmds
|
|
|
|
pipe.cmds = nil
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2016-04-09 10:47:15 +03:00
|
|
|
return cmds, pipe.exec(cmds)
|
|
|
|
}
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2016-04-09 10:47:15 +03:00
|
|
|
func (pipe *Pipeline) pipelined(fn func(*Pipeline) error) ([]Cmder, error) {
|
|
|
|
if err := fn(pipe); err != nil {
|
|
|
|
return nil, err
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2016-04-09 10:47:15 +03:00
|
|
|
cmds, err := pipe.Exec()
|
|
|
|
_ = pipe.Close()
|
|
|
|
return cmds, err
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2016-03-12 11:52:13 +03:00
|
|
|
func execCmds(cn *pool.Conn, cmds []Cmder) ([]Cmder, error) {
|
|
|
|
if err := writeCmd(cn, cmds...); err != nil {
|
2014-05-11 11:42:40 +04:00
|
|
|
setCmdsErr(cmds, err)
|
2015-06-04 11:50:24 +03:00
|
|
|
return cmds, err
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-11 11:42:40 +04:00
|
|
|
var firstCmdErr error
|
2015-06-04 11:50:24 +03:00
|
|
|
var failedCmds []Cmder
|
2015-05-10 15:33:04 +03:00
|
|
|
for _, cmd := range cmds {
|
2015-10-07 17:09:20 +03:00
|
|
|
err := cmd.readReply(cn)
|
2015-06-04 11:50:24 +03:00
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if firstCmdErr == nil {
|
2015-03-18 13:41:24 +03:00
|
|
|
firstCmdErr = err
|
|
|
|
}
|
2015-06-04 11:50:24 +03:00
|
|
|
if shouldRetry(err) {
|
|
|
|
failedCmds = append(failedCmds, cmd)
|
|
|
|
}
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
return failedCmds, firstCmdErr
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|