38 lines
989 B
Python
38 lines
989 B
Python
|
|
from collections import OrderedDict
|
||
|
|
import time
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class MessengerMessageDeduplicator:
|
||
|
|
def __init__(self, max_size: int = 10000, ttl_seconds: int = 300):
|
||
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
||
|
|
self._max_size = max_size
|
||
|
|
self._ttl_seconds = ttl_seconds
|
||
|
|
|
||
|
|
def is_duplicate(self, msg_id: str) -> bool:
|
||
|
|
if not msg_id:
|
||
|
|
return False
|
||
|
|
|
||
|
|
self._evict_expired()
|
||
|
|
|
||
|
|
if msg_id in self._cache:
|
||
|
|
logger.debug(f"messenger dedupe: duplicate {msg_id}")
|
||
|
|
return True
|
||
|
|
|
||
|
|
self._cache[msg_id] = time.monotonic()
|
||
|
|
|
||
|
|
if len(self._cache) > self._max_size:
|
||
|
|
self._cache.popitem(last=False)
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
def _evict_expired(self):
|
||
|
|
now = time.monotonic()
|
||
|
|
cutoff = now - self._ttl_seconds
|
||
|
|
|
||
|
|
expired = [k for k, v in self._cache.items() if v < cutoff]
|
||
|
|
for k in expired:
|
||
|
|
del self._cache[k]
|