-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
14 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import asyncio | ||
from typing import Set | ||
|
||
from cachetools import TTLCache | ||
|
||
from core.events.base import Event | ||
|
||
|
||
class EventDedup: | ||
def __init__(self): | ||
self._events_in_queue: Set[int] = set() | ||
def __init__(self, ttl: int = 10, maxsize: int = 2048): | ||
self._events_in_queue = TTLCache(maxsize=maxsize, ttl=ttl) | ||
self._lock = asyncio.Lock() | ||
|
||
async def add_event(self, event: Event) -> bool: | ||
async def acquire(self, event: Event) -> bool: | ||
async with self._lock: | ||
key = event.meta.key | ||
|
||
if key in self._events_in_queue: | ||
return False | ||
|
||
self._events_in_queue.add(key) | ||
|
||
self._events_in_queue[key] = True | ||
return True | ||
|
||
async def remove_event(self, event: Event) -> None: | ||
async def release(self, event: Event) -> None: | ||
async with self._lock: | ||
self._events_in_queue.discard(event.meta.key) | ||
self._events_in_queue.pop(event.meta.key, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters