redis/pipeline.go

148 lines
3.3 KiB
Go
Raw Permalink Normal View History

package redis
2015-11-04 15:25:48 +03:00
import (
2019-06-04 13:30:47 +03:00
"context"
2015-11-04 15:25:48 +03:00
"sync"
2020-03-11 17:29:16 +03:00
"github.com/go-redis/redis/v8/internal/pool"
2015-11-04 15:25:48 +03:00
)
2019-06-04 13:30:47 +03:00
type pipelineExecer func(context.Context, []Cmder) error
2016-12-13 18:28:39 +03:00
// Pipeliner is an mechanism to realise Redis Pipeline technique.
//
// Pipelining is a technique to extremely speed up processing by packing
// operations to batches, send them at once to Redis and read a replies in a
// singe step.
// See https://redis.io/topics/pipelining
//
// Pay attention, that Pipeline is not a transaction, so you can get unexpected
// results in case of big pipelines and small read/write timeouts.
// Redis client has retransmission logic in case of timeouts, pipeline
// can be retransmitted and commands can be executed more then once.
// To avoid this: it is good idea to use reasonable bigger read/write timeouts
// depends of your batch size and/or use TxPipeline.
2017-05-02 18:00:53 +03:00
type Pipeliner interface {
StatefulCmdable
Len() int
2020-03-11 17:26:42 +03:00
Do(ctx context.Context, args ...interface{}) *Cmd
Process(ctx context.Context, cmd Cmder) error
Close() error
Discard() error
2020-03-11 17:26:42 +03:00
Exec(ctx context.Context) ([]Cmder, error)
}
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 {
2019-05-31 17:03:20 +03:00
cmdable
statefulCmdable
2014-05-11 11:42:40 +04:00
2019-07-25 13:28:15 +03:00
ctx context.Context
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
}
2019-05-31 17:03:20 +03:00
func (c *Pipeline) init() {
c.cmdable = c.Process
c.statefulCmdable = c.Process
}
// Len returns the number of queued commands.
func (c *Pipeline) Len() int {
c.mu.Lock()
ln := len(c.cmds)
c.mu.Unlock()
return ln
}
// Do queues the custom command for later execution.
2020-03-11 17:26:42 +03:00
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
cmd := NewCmd(ctx, args...)
_ = c.Process(ctx, cmd)
2019-02-08 14:22:46 +03:00
return cmd
}
2018-07-12 15:57:03 +03:00
// Process queues the cmd for later execution.
2020-03-11 17:26:42 +03:00
func (c *Pipeline) Process(ctx context.Context, 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.
func (c *Pipeline) Close() error {
2016-10-13 12:11:58 +03:00
c.mu.Lock()
2019-07-25 13:53:00 +03:00
_ = c.discard()
2016-10-13 12:11:58 +03:00
c.closed = true
c.mu.Unlock()
return nil
}
2015-06-04 11:50:24 +03:00
// Discard resets the pipeline and discards queued commands.
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 {
return pool.ErrClosed
2014-05-11 11:42:40 +04: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.
2020-03-11 17:26:42 +03:00
func (c *Pipeline) Exec(ctx context.Context) ([]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
}
if len(c.cmds) == 0 {
return nil, nil
2015-03-18 13:41:24 +03:00
}
2014-05-11 11:42:40 +04:00
cmds := c.cmds
c.cmds = nil
2019-06-04 13:30:47 +03:00
return cmds, c.exec(ctx, cmds)
}
2020-03-11 17:26:42 +03:00
func (c *Pipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
if err := fn(c); err != nil {
return nil, err
}
2020-03-11 17:26:42 +03:00
cmds, err := c.Exec(ctx)
_ = c.Close()
return cmds, err
}
2017-05-02 18:00:53 +03:00
func (c *Pipeline) Pipeline() Pipeliner {
return c
}
2017-09-25 11:48:44 +03:00
2020-03-11 17:26:42 +03:00
func (c *Pipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
return c.Pipelined(ctx, fn)
2017-09-25 11:48:44 +03:00
}
func (c *Pipeline) TxPipeline() Pipeliner {
return c
}