Skip to content

Commit d34fb5e

Browse files
committed
Add max_tries to transaction()
1 parent 50b9e73 commit d34fb5e

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

valkey/asyncio/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ async def transaction(
437437
shard_hint: Optional[str] = None,
438438
value_from_callable: bool = False,
439439
watch_delay: Optional[float] = None,
440+
max_tries: Optional[int] = None,
440441
):
441442
"""
442443
Convenience method for executing the callable `func` as a transaction
@@ -445,7 +446,11 @@ async def transaction(
445446
"""
446447
pipe: Pipeline
447448
async with self.pipeline(True, shard_hint) as pipe:
449+
tries = 0
448450
while True:
451+
tries += 1
452+
if max_tries and max_tries>0 and tries>max_tries:
453+
raise ValkeyError(f"Bailing out of transaction after {tries-1} tries")
449454
try:
450455
if watches:
451456
await pipe.watch(*watches)

valkey/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,13 @@ def transaction(
408408
shard_hint = kwargs.pop("shard_hint", None)
409409
value_from_callable = kwargs.pop("value_from_callable", False)
410410
watch_delay = kwargs.pop("watch_delay", None)
411+
max_tries = kwargs.pop("max_tries", None)
411412
with self.pipeline(True, shard_hint) as pipe:
413+
tries = 0
412414
while True:
415+
tries += 1
416+
if max_tries and max_tries>0 and tries>max_tries:
417+
raise ValkeyError(f"Bailing out of transaction after {tries-1} tries")
413418
try:
414419
if watches:
415420
pipe.watch(*watches)

0 commit comments

Comments
 (0)