mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
Update and rename logger_rate_limiter.py to stream_with_rate_limiter.py
This commit is contained in:
parent
2c1df477e2
commit
db7be11ea0
1 changed files with 3 additions and 4 deletions
27
queues/stream_with_rate_limiter.py
Normal file
27
queues/stream_with_rate_limiter.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/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, message) -> 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
|
||||
|
||||
return False
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue