Merge pull request #1615 from adriansmares/master

Ensure Watch closes the transaction on panic
This commit is contained in:
Vladimir Mihailenco 2021-01-22 09:22:34 +02:00 committed by GitHub
commit 794e5c09aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 5 deletions

7
tx.go
View File

@ -65,16 +65,13 @@ func (c *Tx) Process(ctx context.Context, cmd Cmder) error {
// The transaction is automatically closed when fn exits.
func (c *Client) Watch(ctx context.Context, fn func(*Tx) error, keys ...string) error {
tx := c.newTx(ctx)
defer tx.Close(ctx)
if len(keys) > 0 {
if err := tx.Watch(ctx, keys...).Err(); err != nil {
_ = tx.Close(ctx)
return err
}
}
err := fn(tx)
_ = tx.Close(ctx)
return err
return fn(tx)
}
// Close closes the transaction, releasing any open resources.