forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/pipeline.go

189 lines
3.3 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"errors"
)
type pipelineExecer func(context.Context, []Cmder) error
// Pipeliner is an mechanism to realise Redis Pipeline technique.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Pipelining is a technique to extremely speed up processing by packing
2024-02-18 10:42:21 +00:00
// operations to batches, send them at once to Redis and read a replies in a
2024-02-18 10:42:21 +00:00
// single step.
2024-02-18 10:42:21 +00:00
// See https://redis.io/topics/pipelining
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Pay attention, that Pipeline is not a transaction, so you can get unexpected
2024-02-18 10:42:21 +00:00
// results in case of big pipelines and small read/write timeouts.
2024-02-18 10:42:21 +00:00
// Redis client has retransmission logic in case of timeouts, pipeline
2024-02-18 10:42:21 +00:00
// can be retransmitted and commands can be executed more then once.
2024-02-18 10:42:21 +00:00
// To avoid this: it is good idea to use reasonable bigger read/write timeouts
2024-02-18 10:42:21 +00:00
// depends of your batch size and/or use TxPipeline.
2024-02-18 10:42:21 +00:00
type Pipeliner interface {
StatefulCmdable
// Len is to obtain the number of commands in the pipeline that have not yet been executed.
2024-02-18 10:42:21 +00:00
Len() int
// Do is an API for executing any command.
2024-02-18 10:42:21 +00:00
// If a certain Redis command is not yet supported, you can use Do to execute it.
2024-02-18 10:42:21 +00:00
Do(ctx context.Context, args ...interface{}) *Cmd
// Process is to put the commands to be executed into the pipeline buffer.
2024-02-18 10:42:21 +00:00
Process(ctx context.Context, cmd Cmder) error
// Discard is to discard all commands in the cache that have not yet been executed.
2024-02-18 10:42:21 +00:00
Discard()
// Exec is to send all the commands buffered in the pipeline to the redis-server.
2024-02-18 10:42:21 +00:00
Exec(ctx context.Context) ([]Cmder, error)
}
var _ Pipeliner = (*Pipeline)(nil)
// Pipeline implements pipelining as described in
2024-02-18 10:42:21 +00:00
// http://redis.io/topics/pipelining.
2024-02-18 10:42:21 +00:00
// Please note: it is not safe for concurrent use by multiple goroutines.
2024-02-18 10:42:21 +00:00
type Pipeline struct {
cmdable
2024-02-18 10:42:21 +00:00
statefulCmdable
exec pipelineExecer
2024-02-18 10:42:21 +00:00
cmds []Cmder
}
func (c *Pipeline) init() {
2024-02-18 10:42:21 +00:00
c.cmdable = c.Process
2024-02-18 10:42:21 +00:00
c.statefulCmdable = c.Process
2024-02-18 10:42:21 +00:00
}
// Len returns the number of queued commands.
2024-02-18 10:42:21 +00:00
func (c *Pipeline) Len() int {
2024-02-18 10:42:21 +00:00
return len(c.cmds)
2024-02-18 10:42:21 +00:00
}
// Do queues the custom command for later execution.
2024-02-18 10:42:21 +00:00
func (c *Pipeline) Do(ctx context.Context, args ...interface{}) *Cmd {
2024-02-18 10:42:21 +00:00
cmd := NewCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
if len(args) == 0 {
2024-02-18 10:42:21 +00:00
cmd.SetErr(errors.New("redis: please enter the command to be executed"))
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = c.Process(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
// Process queues the cmd for later execution.
2024-02-18 10:42:21 +00:00
func (c *Pipeline) Process(ctx context.Context, cmd Cmder) error {
2024-02-18 10:42:21 +00:00
c.cmds = append(c.cmds, cmd)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// Discard resets the pipeline and discards queued commands.
2024-02-18 10:42:21 +00:00
func (c *Pipeline) Discard() {
2024-02-18 10:42:21 +00:00
c.cmds = c.cmds[:0]
2024-02-18 10:42:21 +00:00
}
// Exec executes all previously queued commands using one
2024-02-18 10:42:21 +00:00
// client-server roundtrip.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Exec always returns list of commands and error of the first failed
2024-02-18 10:42:21 +00:00
// command if any.
2024-02-18 10:42:21 +00:00
func (c *Pipeline) Exec(ctx context.Context) ([]Cmder, error) {
2024-02-18 10:42:21 +00:00
if len(c.cmds) == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
cmds := c.cmds
2024-02-18 10:42:21 +00:00
c.cmds = nil
return cmds, c.exec(ctx, cmds)
2024-02-18 10:42:21 +00:00
}
func (c *Pipeline) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
2024-02-18 10:42:21 +00:00
if err := fn(c); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c.Exec(ctx)
2024-02-18 10:42:21 +00:00
}
func (c *Pipeline) Pipeline() Pipeliner {
2024-02-18 10:42:21 +00:00
return c
2024-02-18 10:42:21 +00:00
}
func (c *Pipeline) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
2024-02-18 10:42:21 +00:00
return c.Pipelined(ctx, fn)
2024-02-18 10:42:21 +00:00
}
func (c *Pipeline) TxPipeline() Pipeliner {
2024-02-18 10:42:21 +00:00
return c
2024-02-18 10:42:21 +00:00
}