Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions redis/backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,79 @@ def reset(self):
pass

@abstractmethod
def compute(self, failures):
def compute(self, failures: int) -> float:
"""Compute backoff in seconds upon failure"""
pass


class ConstantBackoff(AbstractBackoff):
"""Constant backoff upon failure"""

def __init__(self, backoff):
def __init__(self, backoff: float) -> None:
"""`backoff`: backoff time in seconds"""
self._backoff = backoff

def compute(self, failures):
def compute(self, failures: int) -> float:
return self._backoff


class NoBackoff(ConstantBackoff):
"""No backoff upon failure"""

def __init__(self):
def __init__(self) -> None:
super().__init__(0)


class ExponentialBackoff(AbstractBackoff):
"""Exponential backoff upon failure"""

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

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


class FullJitterBackoff(AbstractBackoff):
"""Full jitter backoff upon failure"""

def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
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):
def compute(self, failures: int) -> float:
return random.uniform(0, min(self._cap, self._base * 2**failures))


class EqualJitterBackoff(AbstractBackoff):
"""Equal jitter backoff upon failure"""

def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
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):
def compute(self, failures: int) -> float:
temp = min(self._cap, self._base * 2**failures) / 2
return temp + random.uniform(0, temp)


class DecorrelatedJitterBackoff(AbstractBackoff):
"""Decorrelated jitter backoff upon failure"""

def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
def __init__(self, cap: float = DEFAULT_CAP, base: float = DEFAULT_BASE) -> None:
"""
`cap`: maximum backoff time in seconds
`base`: base backoff time in seconds
Expand All @@ -100,10 +100,10 @@ def __init__(self, cap=DEFAULT_CAP, base=DEFAULT_BASE):
self._base = base
self._previous_backoff = 0

def reset(self):
def reset(self) -> None:
self._previous_backoff = 0

def compute(self, failures):
def compute(self, failures: int) -> float:
max_backoff = max(self._base, self._previous_backoff * 3)
temp = random.uniform(self._base, max_backoff)
self._previous_backoff = min(self._cap, temp)
Expand Down