ForcePilot/backend/package/yuxi/channels/adapters/whatsapp/debounce.py
Kris e9b57546ea feat(whatsapp): 新增WhatsApp适配器完整功能模块
新增大量WhatsApp适配器相关代码,包括账号管理、会话处理、消息收发、验证授权、媒体处理、互动命令、审批流程、健康检测等完整功能模块,搭建基础的Baileys协议WhatsApp接入能力
2026-05-12 00:51:58 +08:00

45 lines
1.5 KiB
Python

from __future__ import annotations
import time
from collections import defaultdict
from threading import Lock
class MessageDebouncer:
def __init__(self, window_seconds: float = 2.0, max_calls: int = 3):
self._window = window_seconds
self._max_calls = max_calls
self._last_send: defaultdict[str, list[float]] = defaultdict(list)
self._lock = Lock()
def should_throttle(self, remote_jid: str) -> bool:
now = time.monotonic()
with self._lock:
times = self._last_send.get(remote_jid, [])
times = [t for t in times if now - t < self._window]
if len(times) >= self._max_calls:
return True
times.append(now)
self._last_send[remote_jid] = times
return False
def record_send(self, remote_jid: str) -> None:
now = time.monotonic()
with self._lock:
self._last_send[remote_jid].append(now)
def window_remaining(self, remote_jid: str) -> float:
now = time.monotonic()
with self._lock:
times = [t for t in self._last_send.get(remote_jid, []) if now - t < self._window]
if len(times) >= self._max_calls and times:
return max(0.0, self._window - (now - times[0]))
return 0.0
def clear(self, remote_jid: str | None = None) -> None:
with self._lock:
if remote_jid:
self._last_send.pop(remote_jid, None)
else:
self._last_send.clear()