2012-08-11 18:42:10 +04:00
|
|
|
package redis
|
|
|
|
|
2015-11-04 15:25:48 +03:00
|
|
|
import (
|
|
|
|
"sync"
|
2016-03-12 11:52:13 +03:00
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/internal/pool"
|
2015-11-04 15:25:48 +03:00
|
|
|
)
|
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
type pipelineExecer func([]Cmder) error
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
type Pipeliner interface {
|
2017-05-01 18:42:58 +03:00
|
|
|
StatefulCmdable
|
|
|
|
Process(cmd Cmder) error
|
|
|
|
Close() error
|
|
|
|
Discard() error
|
|
|
|
discard() error
|
|
|
|
Exec() ([]Cmder, error)
|
2017-05-02 18:00:53 +03:00
|
|
|
pipelined(fn func(Pipeliner) error) ([]Cmder, error)
|
2017-05-01 18:42:58 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 18:41:42 +03:00
|
|
|
var _ Pipeliner = (*Pipeline)(nil)
|
|
|
|
|
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 {
|
2016-06-05 12:45:39 +03:00
|
|
|
statefulCmdable
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2016-12-13 18:28:39 +03:00
|
|
|
exec pipelineExecer
|
2015-06-04 11:50:24 +03:00
|
|
|
|
2016-10-13 12:11:58 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
cmds []Cmder
|
|
|
|
closed bool
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Pipeline) Process(cmd Cmder) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.cmds = append(c.cmds, cmd)
|
|
|
|
c.mu.Unlock()
|
2016-06-17 15:09:38 +03:00
|
|
|
return nil
|
2015-01-24 15:12:48 +03:00
|
|
|
}
|
|
|
|
|
2015-09-12 09:36:03 +03:00
|
|
|
// Close closes the pipeline, releasing any open resources.
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Pipeline) Close() error {
|
2016-10-13 12:11:58 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
c.discard()
|
|
|
|
c.closed = true
|
|
|
|
c.mu.Unlock()
|
2012-08-11 18:42:10 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-04 11:50:24 +03:00
|
|
|
// Discard resets the pipeline and discards queued commands.
|
2016-07-01 15:25:28 +03:00
|
|
|
func (c *Pipeline) Discard() error {
|
|
|
|
c.mu.Lock()
|
2016-10-13 12:11:58 +03:00
|
|
|
err := c.discard()
|
|
|
|
c.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Pipeline) discard() error {
|
|
|
|
if c.closed {
|
2016-03-14 14:17:33 +03:00
|
|
|
return pool.ErrClosed
|
2014-05-11 11:42:40 +04:00
|
|
|
}
|
2016-07-01 15:25:28 +03:00
|
|
|
c.cmds = c.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-07-01 15:25:28 +03:00
|
|
|
func (c *Pipeline) Exec() ([]Cmder, error) {
|
|
|
|
c.mu.Lock()
|
2017-01-28 11:53:10 +03:00
|
|
|
defer c.mu.Unlock()
|
2015-11-04 15:25:48 +03:00
|
|
|
|
2016-10-13 12:11:58 +03:00
|
|
|
if c.closed {
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
if len(c.cmds) == 0 {
|
2017-05-30 15:45:36 +03:00
|
|
|
return nil, nil
|
2015-03-18 13:41:24 +03:00
|
|
|
}
|
2014-05-11 11:42:40 +04:00
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
cmds := c.cmds
|
|
|
|
c.cmds = nil
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2016-07-01 15:25:28 +03:00
|
|
|
return cmds, c.exec(cmds)
|
2016-04-09 10:47:15 +03:00
|
|
|
}
|
2015-05-10 15:33:04 +03:00
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Pipeline) pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2016-07-01 15:25:28 +03:00
|
|
|
if err := fn(c); err != nil {
|
2016-04-09 10:47:15 +03:00
|
|
|
return nil, err
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2016-07-01 15:25:28 +03:00
|
|
|
cmds, err := c.Exec()
|
|
|
|
_ = c.Close()
|
2016-04-09 10:47:15 +03:00
|
|
|
return cmds, err
|
2012-08-11 18:42:10 +04:00
|
|
|
}
|
2017-05-01 18:42:58 +03:00
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Pipeline) Pipelined(fn func(Pipeliner) error) ([]Cmder, error) {
|
2017-05-01 18:42:58 +03:00
|
|
|
return c.pipelined(fn)
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
func (c *Pipeline) Pipeline() Pipeliner {
|
2017-05-01 18:42:58 +03:00
|
|
|
return c
|
|
|
|
}
|