mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
28
queues/logger_rate_limiter.py
Normal file
28
queues/logger_rate_limiter.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
class Logger:
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.msg_set = set()
|
||||
self.msg_queue = deque()
|
||||
|
||||
def print_message(self, timestamp: int, message: str) -> bool:
|
||||
|
||||
while self.msg_queue:
|
||||
msg, msg_timestamp = self.msg_queue[0]
|
||||
if timestamp - msg_timestamp >= 10:
|
||||
self.msg_queue.popleft()
|
||||
self.msg_set.remove(msg)
|
||||
else:
|
||||
break
|
||||
|
||||
if message not in self.msg_set:
|
||||
self.msg_set.add(message)
|
||||
self.msg_queue.append((message, timestamp))
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue