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

258 lines
4.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"github.com/redis/go-redis/v9/internal/pool"
"github.com/redis/go-redis/v9/internal/proto"
)
// TxFailedErr transaction redis failed.
2024-02-18 10:42:21 +00:00
const TxFailedErr = proto.RedisError("redis: transaction failed")
// Tx implements Redis transactions as described in
2024-02-18 10:42:21 +00:00
// http://redis.io/topics/transactions. It's NOT safe for concurrent use
2024-02-18 10:42:21 +00:00
// by multiple goroutines, because Exec resets list of watched keys.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// If you don't need WATCH, use Pipeline instead.
2024-02-18 10:42:21 +00:00
type Tx struct {
baseClient
2024-02-18 10:42:21 +00:00
cmdable
2024-02-18 10:42:21 +00:00
statefulCmdable
2024-02-18 10:42:21 +00:00
hooksMixin
}
func (c *Client) newTx() *Tx {
2024-02-18 10:42:21 +00:00
tx := Tx{
2024-02-18 10:42:21 +00:00
baseClient: baseClient{
opt: c.opt,
2024-02-18 10:42:21 +00:00
connPool: pool.NewStickyConnPool(c.connPool),
},
2024-02-18 10:42:21 +00:00
hooksMixin: c.hooksMixin.clone(),
}
2024-02-18 10:42:21 +00:00
tx.init()
2024-02-18 10:42:21 +00:00
return &tx
2024-02-18 10:42:21 +00:00
}
func (c *Tx) 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
c.initHooks(hooks{
dial: c.baseClient.dial,
process: c.baseClient.process,
pipeline: c.baseClient.processPipeline,
2024-02-18 10:42:21 +00:00
txPipeline: c.baseClient.processTxPipeline,
})
2024-02-18 10:42:21 +00:00
}
func (c *Tx) Process(ctx context.Context, cmd Cmder) error {
2024-02-18 10:42:21 +00:00
err := c.processHook(ctx, cmd)
2024-02-18 10:42:21 +00:00
cmd.SetErr(err)
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Watch prepares a transaction and marks the keys to be watched
2024-02-18 10:42:21 +00:00
// for conditional execution if there are any keys.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The transaction is automatically closed when fn exits.
2024-02-18 10:42:21 +00:00
func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
2024-02-18 10:42:21 +00:00
tx := c.newTx()
2024-02-18 10:42:21 +00:00
defer tx.Close(ctx)
2024-02-18 10:42:21 +00:00
if len(keys) > 0 {
2024-02-18 10:42:21 +00:00
if err := tx.Watch(ctx, keys...).Err(); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fn(tx)
2024-02-18 10:42:21 +00:00
}
// Close closes the transaction, releasing any open resources.
2024-02-18 10:42:21 +00:00
func (c *Tx) Close(ctx context.Context) error {
2024-02-18 10:42:21 +00:00
_ = c.Unwatch(ctx).Err()
2024-02-18 10:42:21 +00:00
return c.baseClient.Close()
2024-02-18 10:42:21 +00:00
}
// Watch marks the keys to be watched for conditional execution
2024-02-18 10:42:21 +00:00
// of a transaction.
2024-02-18 10:42:21 +00:00
func (c *Tx) Watch(ctx context.Context, keys ...string) *StatusCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 1+len(keys))
2024-02-18 10:42:21 +00:00
args[0] = "watch"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[1+i] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(ctx, args...)
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
}
// Unwatch flushes all the previously watched keys for a transaction.
2024-02-18 10:42:21 +00:00
func (c *Tx) Unwatch(ctx context.Context, keys ...string) *StatusCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 1+len(keys))
2024-02-18 10:42:21 +00:00
args[0] = "unwatch"
2024-02-18 10:42:21 +00:00
for i, key := range keys {
2024-02-18 10:42:21 +00:00
args[1+i] = key
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(ctx, args...)
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
}
// Pipeline creates a pipeline. Usually it is more convenient to use Pipelined.
2024-02-18 10:42:21 +00:00
func (c *Tx) Pipeline() Pipeliner {
2024-02-18 10:42:21 +00:00
pipe := Pipeline{
2024-02-18 10:42:21 +00:00
exec: func(ctx context.Context, cmds []Cmder) error {
2024-02-18 10:42:21 +00:00
return c.processPipelineHook(ctx, cmds)
2024-02-18 10:42:21 +00:00
},
}
2024-02-18 10:42:21 +00:00
pipe.init()
2024-02-18 10:42:21 +00:00
return &pipe
2024-02-18 10:42:21 +00:00
}
// Pipelined executes commands queued in the fn outside of the transaction.
2024-02-18 10:42:21 +00:00
// Use TxPipelined if you need transactional behavior.
2024-02-18 10:42:21 +00:00
func (c *Tx) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
2024-02-18 10:42:21 +00:00
return c.Pipeline().Pipelined(ctx, fn)
2024-02-18 10:42:21 +00:00
}
// TxPipelined executes commands queued in the fn in the transaction.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// When using WATCH, EXEC will execute commands only if the watched keys
2024-02-18 10:42:21 +00:00
// were not modified, allowing for a check-and-set mechanism.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Exec always returns list of commands. If transaction fails
2024-02-18 10:42:21 +00:00
// TxFailedErr is returned. Otherwise Exec returns an error of the first
2024-02-18 10:42:21 +00:00
// failed command or nil.
2024-02-18 10:42:21 +00:00
func (c *Tx) TxPipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) {
2024-02-18 10:42:21 +00:00
return c.TxPipeline().Pipelined(ctx, fn)
2024-02-18 10:42:21 +00:00
}
// TxPipeline creates a pipeline. Usually it is more convenient to use TxPipelined.
2024-02-18 10:42:21 +00:00
func (c *Tx) TxPipeline() Pipeliner {
2024-02-18 10:42:21 +00:00
pipe := Pipeline{
2024-02-18 10:42:21 +00:00
exec: func(ctx context.Context, cmds []Cmder) error {
2024-02-18 10:42:21 +00:00
cmds = wrapMultiExec(ctx, cmds)
2024-02-18 10:42:21 +00:00
return c.processTxPipelineHook(ctx, cmds)
2024-02-18 10:42:21 +00:00
},
}
2024-02-18 10:42:21 +00:00
pipe.init()
2024-02-18 10:42:21 +00:00
return &pipe
2024-02-18 10:42:21 +00:00
}
func wrapMultiExec(ctx context.Context, cmds []Cmder) []Cmder {
2024-02-18 10:42:21 +00:00
if len(cmds) == 0 {
2024-02-18 10:42:21 +00:00
panic("not reached")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmdsCopy := make([]Cmder, len(cmds)+2)
2024-02-18 10:42:21 +00:00
cmdsCopy[0] = NewStatusCmd(ctx, "multi")
2024-02-18 10:42:21 +00:00
copy(cmdsCopy[1:], cmds)
2024-02-18 10:42:21 +00:00
cmdsCopy[len(cmdsCopy)-1] = NewSliceCmd(ctx, "exec")
2024-02-18 10:42:21 +00:00
return cmdsCopy
2024-02-18 10:42:21 +00:00
}