Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing type hints for backoff.py #3249

Merged
merged 1 commit into from
May 28, 2024
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
Loading