Skip to content

Commit

Permalink
Allow delay between watch errors
Browse files Browse the repository at this point in the history
When a watcher error occurs (due to some key being
watched being mutated) the current behavior is to
immediately try again. To avoid the thundering herd
problem a delay is nice to provide to avoid these
situations by introducing a sleep period between these
types of failures.
  • Loading branch information
Joshua Harlow committed Dec 9, 2014
1 parent 38b19cd commit 9cea9e8
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import sys
import warnings
import time
import threading
import time as mod_time
from redis._compat import (b, basestring, bytes, imap, iteritems, iterkeys,
Expand Down Expand Up @@ -477,6 +478,7 @@ def transaction(self, func, *watches, **kwargs):
"""
shard_hint = kwargs.pop('shard_hint', None)
value_from_callable = kwargs.pop('value_from_callable', False)
watch_delay = kwargs.pop('watch_delay', None)
with self.pipeline(True, shard_hint) as pipe:
while 1:
try:
Expand All @@ -486,6 +488,8 @@ def transaction(self, func, *watches, **kwargs):
exec_value = pipe.execute()
return func_value if value_from_callable else exec_value
except WatchError:
if watch_delay is not None and watch_delay > 0:
time.sleep(watch_delay)
continue

def lock(self, name, timeout=None, sleep=0.1, blocking_timeout=None,
Expand Down

0 comments on commit 9cea9e8

Please sign in to comment.