ForcePilot/backend/package/yuxi/channel/extensions/generic_webhook/dedupe.py
Kris 71e90075e2 feat(generic-webhook): 新增通用Webhook管道插件
实现了完整的通用Webhook通道插件,支持入站请求接收、JSONPath字段映射、多种认证方式、出站Webhook推送,以及端点配置管理、去重、安全校验等功能
2026-05-21 10:47:58 +08:00

54 lines
1.5 KiB
Python

import logging
import time
from collections import OrderedDict
logger = logging.getLogger("yuxi.channel.generic_webhook.dedupe")
DEFAULT_TTL = 600
DEFAULT_MAX_SIZE = 10000
class WebhookDeduplicator:
def __init__(self, max_size: int = DEFAULT_MAX_SIZE, ttl_seconds: int = DEFAULT_TTL):
self._cache: OrderedDict[str, float] = OrderedDict()
self._max_size = max_size
self._ttl = ttl_seconds
def is_duplicate(self, endpoint_id: str, event_id: str) -> bool:
if not event_id:
return False
key = f"{endpoint_id}:{event_id}"
now = time.monotonic()
self._evict_expired(now)
if key in self._cache:
return True
self._cache[key] = now
while len(self._cache) > self._max_size:
self._cache.popitem(last=False)
return False
def _evict_expired(self, now: float):
threshold = now - self._ttl
while self._cache:
_, t = next(iter(self._cache.items()))
if t >= threshold:
break
self._cache.popitem(last=False)
def reset(self, endpoint_id: str | None = None):
if endpoint_id:
prefix = f"{endpoint_id}:"
keys_to_remove = [k for k in self._cache if k.startswith(prefix)]
for k in keys_to_remove:
del self._cache[k]
else:
self._cache.clear()
@property
def size(self) -> int:
return len(self._cache)