Skip to content

Commit

Permalink
Add missing type hints for backoff.py (#3249)
Browse files Browse the repository at this point in the history
Add type hints to backoff.py
  • Loading branch information
max-muoto authored and gerzse committed Jul 11, 2024
1 parent 271cdaf commit bab48a9
Showing 1 changed file with 13 additions and 13 deletions.
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

0 comments on commit bab48a9

Please sign in to comment.