新增 Signal 渠道扩展,支持在 Yuxi 平台中集成 Signal 加密即时通讯渠道。 包含以下功能模块: - client: Signal 客户端封装 - daemon: signald 守护进程管理 - config_schema: 配置模式 - send: 消息发送 - accounts: 账户管理 - account_management: 账户综合管理 - access_policy: 访问策略 - identity: 身份管理 - profiles: 用户资料 - groups: 群组管理 - format: 消息格式转换 - normalize: 消息规范化 - dedupe: 消息去重 - monitor: 渠道状态监控 - probe: 健康探测 - sse_reconnect: SSE 重连机制
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
import asyncio
|
|
import logging
|
|
import random
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SseReconnector:
|
|
def __init__(
|
|
self,
|
|
initial_ms: float = 1000,
|
|
max_ms: float = 10000,
|
|
factor: float = 2.0,
|
|
jitter: float = 0.2,
|
|
):
|
|
self.initial_ms = initial_ms
|
|
self.max_ms = max_ms
|
|
self.factor = factor
|
|
self.jitter = jitter
|
|
self._attempts = 0
|
|
|
|
def reset(self) -> None:
|
|
self._attempts = 0
|
|
|
|
async def wait_before_reconnect(self, cancel: asyncio.Event) -> None:
|
|
delay = self.initial_ms * (self.factor**self._attempts)
|
|
delay = min(delay, self.max_ms)
|
|
jitter_amount = delay * random.uniform(-self.jitter, self.jitter)
|
|
delay_ms = max(0, delay + jitter_amount) / 1000
|
|
self._attempts += 1
|
|
|
|
logger.info("Signal SSE reconnecting in %.1fs (attempt %d)", delay_ms, self._attempts)
|
|
try:
|
|
await asyncio.wait_for(cancel.wait(), timeout=delay_ms)
|
|
except asyncio.TimeoutError:
|
|
pass
|