Skip to content

Commit

Permalink
Ensure Watch closes the transaction on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansmares committed Jan 21, 2021
1 parent a8e8bf1 commit 4ceab0f
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4ceab0f

Please sign in to comment.