Skip to content

Commit

Permalink
expoential with backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
JimNero009 committed Mar 7, 2025
1 parent 601c1aa commit 91c594b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions redis/backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ def compute(self, failures: int) -> float:
self._previous_backoff = min(self._cap, temp)
return self._previous_backoff

class ExponentialWithJitterBackoff(AbstractBackoff):
"""Exponential backoff upon failure, with jitter"""

def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
"""
`cap`: maximum backoff time in seconds
`base`: base backoff time in seconds
"""
self._cap = cap
self._base = base

def compute(self, failures: int) -> float:
return min(self._cap, random.random() * self._base * 2**failures)


def default_backoff():
return EqualJitterBackoff()

0 comments on commit 91c594b

Please sign in to comment.