d73ff9b0fb
Deploy Site / deploy-vercel (push) Has been cancelled
Deploy Site / deploy-docs (push) Has been cancelled
Docker / shell lint / Lint Dockerfile (hadolint) (push) Has been cancelled
Docker / shell lint / Lint docker/ shell scripts (shellcheck) (push) Has been cancelled
Docker Build and Publish / build-amd64 (push) Has been cancelled
Docker Build and Publish / build-arm64 (push) Has been cancelled
Lint (ruff + ty) / ruff + ty diff (push) Has been cancelled
Lint (ruff + ty) / ruff enforcement (blocking) (push) Has been cancelled
Lint (ruff + ty) / Windows footguns (blocking) (push) Has been cancelled
Nix Lockfile Fix / auto-fix-main (push) Has been cancelled
Nix Lockfile Fix / fix (push) Has been cancelled
Nix / nix (macos-latest) (push) Has been cancelled
Nix / nix (ubuntu-latest) (push) Has been cancelled
OSV-Scanner / Scan lockfiles (push) Has been cancelled
Build Skills Index / build-index (push) Has been cancelled
Tests / test (1) (push) Has been cancelled
Tests / test (2) (push) Has been cancelled
Tests / test (3) (push) Has been cancelled
Tests / test (4) (push) Has been cancelled
Tests / test (5) (push) Has been cancelled
Tests / test (6) (push) Has been cancelled
Tests / e2e (push) Has been cancelled
uv.lock check / uv lock --check (push) Has been cancelled
Docker Build and Publish / merge (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
Tests / save-durations (push) Has been cancelled
58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
"""Retry utilities — jittered backoff for decorrelated retries.
|
|
|
|
Replaces fixed exponential backoff with jittered delays to prevent
|
|
thundering-herd retry spikes when multiple sessions hit the same
|
|
rate-limited provider concurrently.
|
|
"""
|
|
|
|
import random
|
|
import threading
|
|
import time
|
|
|
|
# Monotonic counter for jitter seed uniqueness within the same process.
|
|
# Protected by a lock to avoid race conditions in concurrent retry paths
|
|
# (e.g. multiple gateway sessions retrying simultaneously).
|
|
_jitter_counter = 0
|
|
_jitter_lock = threading.Lock()
|
|
|
|
|
|
def jittered_backoff(
|
|
attempt: int,
|
|
*,
|
|
base_delay: float = 5.0,
|
|
max_delay: float = 120.0,
|
|
jitter_ratio: float = 0.5,
|
|
) -> float:
|
|
"""Compute a jittered exponential backoff delay.
|
|
|
|
Args:
|
|
attempt: 1-based retry attempt number.
|
|
base_delay: Base delay in seconds for attempt 1.
|
|
max_delay: Maximum delay cap in seconds.
|
|
jitter_ratio: Fraction of computed delay to use as random jitter
|
|
range. 0.5 means jitter is uniform in [0, 0.5 * delay].
|
|
|
|
Returns:
|
|
Delay in seconds: min(base * 2^(attempt-1), max_delay) + jitter.
|
|
|
|
The jitter decorrelates concurrent retries so multiple sessions
|
|
hitting the same provider don't all retry at the same instant.
|
|
"""
|
|
global _jitter_counter
|
|
with _jitter_lock:
|
|
_jitter_counter += 1
|
|
tick = _jitter_counter
|
|
|
|
exponent = max(0, attempt - 1)
|
|
if exponent >= 63 or base_delay <= 0:
|
|
delay = max_delay
|
|
else:
|
|
delay = min(base_delay * (2 ** exponent), max_delay)
|
|
|
|
# Seed from time + counter for decorrelation even with coarse clocks.
|
|
seed = (time.time_ns() ^ (tick * 0x9E3779B9)) & 0xFFFFFFFF
|
|
rng = random.Random(seed)
|
|
jitter = rng.uniform(0, jitter_ratio * delay)
|
|
|
|
return delay + jitter
|