2019-04-08 18:05:15 +09:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-09-28 06:09:46 +09:00
|
|
|
"context"
|
2019-04-08 18:05:15 +09:00
|
|
|
"sync"
|
|
|
|
|
2021-02-11 06:28:32 +09:00
|
|
|
"github.com/go-redis/redis/v8/internal/pool"
|
2019-04-08 18:05:15 +09:00
|
|
|
)
|
|
|
|
|
2020-09-28 06:09:46 +09:00
|
|
|
type pipelineExecer func(context.Context, []Cmder) error
|
2019-04-08 18:05:15 +09:00
|
|
|
|
2020-09-28 06:09:46 +09: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.
|
2019-04-08 18:05:15 +09:00
|
|
|
type Pipeliner interface {
|
|
|
|
StatefulCmdable
|
2021-02-11 06:28:32 +09:00
|
|
|
Do(ctx context.Context, args ...interface{}) *Cmd
|
|
|
|
Process(ctx context.Context, cmd Cmder) error
|
2019-04-08 18:05:15 +09:00
|
|
|
Close() error
|
|
|
|
Discard() error
|
2021-02-11 06:28:32 +09:00
|
|
|
Exec(ctx context.Context) ([]Cmder, error)
|
2019-04-08 18:05:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Pipeliner = (*Pipeline)(nil)
|
|
|
|
|
|
|
|
// Pipeline implements pipelining as described in
|
|
|
|
// http://redis.io/topics/pipelining. It's safe for concurrent use
|
|
|
|
// by multiple goroutines.
|
|
|
|
type Pipeline struct {
|
2020-09-28 06:09:46 +09:00
|
|
|
cmdable
|
2019-04-08 18:05:15 +09:00
|
|
|
statefulCmdable
|
|
|
|
|
2020-09-28 06:09:46 +09:00
|
|
|
ctx context.Context
|
2019-04-08 18:05:15 +09:00
|
|
|
exec pipelineExecer
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
cmds []Cmder
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:09:46 +09:00
|
|
|
func (c *Pipeline) init() {
|
|
|
|
c.cmdable = c.Process
|
|
|
|
c.statefulCmdable = c.Process
|
|
|
|
}
|
|
|
|
|
2021-02-11 06:28:32 +09:00
|
|
|
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
|
|
|
|
cmd := NewCmd(ctx, args...)
|
|
|
|
_ = c.Process(ctx, cmd)
|
2019-04-08 18:05:15 +09:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process queues the cmd for later execution.
|
2021-02-11 06:28:32 +09:00
|
|
|
func (c *Pipeline) Process(ctx context.Context, cmd Cmder) error {
|
2019-04-08 18:05:15 +09:00
|
|
|
c.mu.Lock()
|
|
|
|
c.cmds = append(c.cmds, cmd)
|
|
|
|
c.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the pipeline, releasing any open resources.
|
|
|
|
func (c *Pipeline) Close() error {
|
|
|
|
c.mu.Lock()
|
2020-09-28 06:09:46 +09:00
|
|
|
_ = c.discard()
|
2019-04-08 18:05:15 +09:00
|
|
|
c.closed = true
|
|
|
|
c.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard resets the pipeline and discards queued commands.
|
|
|
|
func (c *Pipeline) Discard() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
err := c.discard()
|
|
|
|
c.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Pipeline) discard() error {
|
|
|
|
if c.closed {
|
|
|
|
return pool.ErrClosed
|
|
|
|
}
|
|
|
|
c.cmds = c.cmds[:0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes all previously queued commands using one
|
|
|
|
// client-server roundtrip.
|
|
|
|
//
|
|
|
|
// Exec always returns list of commands and error of the first failed
|
|
|
|
// command if any.
|
2021-02-11 06:28:32 +09:00
|
|
|
func (c *Pipeline) Exec(ctx context.Context) ([]Cmder, error) {
|
2019-04-08 18:05:15 +09:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.closed {
|
|
|
|
return nil, pool.ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.cmds) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmds := c.cmds
|
|
|
|
c.cmds = nil
|
|
|
|
|
2020-09-28 06:09:46 +09:00
|
|
|
return cmds, c.exec(ctx, cmds)
|
2019-04-08 18:05:15 +09:00
|
|
|
}
|
|
|
|
|
2021-02-11 06:28:32 +09:00
|
|
|
func (c *Pipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
|
2019-04-08 18:05:15 +09:00
|
|
|
if err := fn(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-11 06:28:32 +09:00
|
|
|
cmds, err := c.Exec(ctx)
|
2019-04-08 18:05:15 +09:00
|
|
|
_ = c.Close()
|
|
|
|
return cmds, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Pipeline) Pipeline() Pipeliner {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-02-11 06:28:32 +09:00
|
|
|
func (c *Pipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
|
|
|
|
return c.Pipelined(ctx, fn)
|
2019-04-08 18:05:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Pipeline) TxPipeline() Pipeliner {
|
|
|
|
return c
|
|
|
|
}
|