1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

Cancel sleep when context is cancelled

This commit is contained in:
Vladimir Mihailenco
2019-07-30 12:13:00 +03:00
parent 6d8db67ef5
commit c837612911
6 changed files with 58 additions and 16 deletions

10
tx.go
View File

@ -22,13 +22,13 @@ type Tx struct {
ctx context.Context
}
func (c *Client) newTx() *Tx {
func (c *Client) newTx(ctx context.Context) *Tx {
tx := Tx{
baseClient: baseClient{
opt: c.opt,
connPool: pool.NewStickyConnPool(c.connPool.(*pool.ConnPool), true),
},
ctx: c.ctx,
ctx: ctx,
}
tx.init()
return &tx
@ -65,7 +65,11 @@ func (c *Tx) ProcessContext(ctx context.Context, cmd Cmder) error {
//
// The transaction is automatically closed when fn exits.
func (c *Client) Watch(fn func(*Tx) error, keys ...string) error {
tx := c.newTx()
return c.WatchContext(c.ctx, fn, keys...)
}
func (c *Client) WatchContext(ctx context.Context, fn func(*Tx) error, keys ...string) error {
tx := c.newTx(ctx)
if len(keys) > 0 {
if err := tx.Watch(keys...).Err(); err != nil {
_ = tx.Close()