ForcePilot/backend/package/yuxi/channel/lifecycle/backoff.py
Kris bab30f2715
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Ruff Format Check / Ruff Format & Lint (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat:0715
2026-07-15 12:30:58 +08:00

21 lines
686 B
Python

"""指数退避策略"""
import random
class BackoffPolicy:
"""提供带随机抖动的指数退避,用于渠道重连间隔计算。"""
def __init__(self, base_ms: int = 1000, max_ms: int = 30000, jitter_ms: int = 1000):
self.base_ms = base_ms
self.max_ms = max_ms
self.jitter_ms = jitter_ms
def compute(self, attempts: int) -> int:
"""根据已尝试次数返回毫秒级延迟。"""
attempts = max(0, attempts)
capped_attempts = min(attempts, 30)
delay = min(self.base_ms * (2**capped_attempts), self.max_ms)
jitter = random.randint(0, self.jitter_ms)
return min(delay + jitter, self.max_ms)