Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions redis/backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,20 @@ def compute(self, failures: int) -> float:
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()
18 changes: 18 additions & 0 deletions tests/test_backoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from unittest.mock import Mock

import pytest

from redis.backoff import ExponentialWithJitterBackoff


def test_exponential_with_jitter_backoff(monkeypatch: pytest.MonkeyPatch) -> None:
mock_random = Mock(side_effect=[0.25, 0.5, 0.75, 1.0, 0.9])
monkeypatch.setattr("random.random", mock_random)

bo = ExponentialWithJitterBackoff(cap=5, base=1)

assert bo.compute(0) == 0.25 # min(5, 0.25*2^0)
assert bo.compute(1) == 1.0 # min(5, 0.5*2^1)
assert bo.compute(2) == 3.0 # min(5, 0.75*2^2)
assert bo.compute(3) == 5.0 # min(5, 1*2^3)
assert bo.compute(4) == 5.0 # min(5, 0.9*2^4)
Loading