新增大量渠道适配器相关的协议、策略、工具类与基础设施代码,包括: 1. 多协议定义:认证、消息、配置、网关等核心接口 2. 策略模块:上下文、群聊、去重、防抖等业务策略 3. 工具集:重试、去重、文本分块、消息格式化等SDK工具 4. 基础设施:外部进程管理、事件广播、熔断机制等 5. 账户与管道系统:账户管理、消息处理管道实现 6. 运行时服务:状态收集、维护任务、日志等后台服务
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
from collections import OrderedDict
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class DedupeConfig:
|
|
ttl_seconds: float = 300.0
|
|
max_entries: int = 10000
|
|
|
|
|
|
class MessageDeduplicator:
|
|
def __init__(self, config: DedupeConfig | None = None):
|
|
self._config = config or DedupeConfig()
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
self._lock = threading.Lock()
|
|
|
|
def is_duplicate(self, message_id: str) -> bool:
|
|
now = time.monotonic()
|
|
with self._lock:
|
|
self._evict_expired(now)
|
|
if message_id in self._cache:
|
|
return True
|
|
if len(self._cache) >= self._config.max_entries:
|
|
self._cache.popitem(last=False)
|
|
self._cache[message_id] = now
|
|
self._cache.move_to_end(message_id)
|
|
return False
|
|
|
|
def record(self, message_id: str) -> None:
|
|
now = time.monotonic()
|
|
with self._lock:
|
|
self._evict_expired(now)
|
|
self._cache[message_id] = now
|
|
self._cache.move_to_end(message_id)
|
|
|
|
def clear(self) -> None:
|
|
with self._lock:
|
|
self._cache.clear()
|
|
|
|
def _evict_expired(self, now: float) -> None:
|
|
expired = [k for k, t in self._cache.items() if now - t > self._config.ttl_seconds]
|
|
for k in expired:
|
|
del self._cache[k]
|