ForcePilot/backend/package/yuxi/channels/adapters/twitch/deduplicator.py
Kris 59cd13cf84 feat(twitch): 实现完整的Twitch聊天适配器模块
新增Twitch IRC协议相关的全套实现,包括:
1. 基础工具类:令牌处理、消息格式化、速率限制、消息去重
2. 核心适配器组件:IRC解析器、消息归一化、外发消息处理
3. API客户端:Helix API封装、认证提供者
4. 配置与部署:配置校验、设置向导
5. 辅助功能:配对管理、健康检查、目标解析等
2026-05-12 00:50:10 +08:00

49 lines
1.4 KiB
Python

from __future__ import annotations
import time
from collections import OrderedDict
from threading import Lock
class MessageDeduplicator:
TTL_SECONDS = 60.0
def __init__(self, max_size: int = 1000):
self._max_size = max_size
self._cache: OrderedDict[str, float] = OrderedDict()
self._lock = Lock()
def is_duplicate(self, message_id: str) -> bool:
now = time.monotonic()
with self._lock:
if message_id in self._cache:
ts = self._cache[message_id]
if now - ts < self.TTL_SECONDS:
return True
del self._cache[message_id]
self._cache[message_id] = now
self._cache.move_to_end(message_id)
if len(self._cache) > self._max_size:
self._cleanup_expired(now)
return False
def mark_seen(self, message_id: str) -> None:
with self._lock:
self._cache[message_id] = time.monotonic()
self._cache.move_to_end(message_id)
if len(self._cache) > self._max_size:
self._cleanup_expired(time.monotonic())
def _cleanup_expired(self, now: float) -> None:
expired = [k for k, ts in self._cache.items() if now - ts >= self.TTL_SECONDS]
for k in expired:
del self._cache[k]
def __len__(self) -> int:
with self._lock:
return len(self._cache)