Created
January 3, 2023 22:20
-
-
Save PouyaEsmaeili/6b1f0536f2a0ed12472cd0ca37e8dd0f to your computer and use it in GitHub Desktop.
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
def log(self, client_id: str, resource_id: str) -> bool: | |
lock_name = self._get_lock_name(client_id, resource_id) | |
with self._con_pool.lock(name=lock_name, timeout=self._lock_timeout): | |
if self.is_allowed(client_id, resource_id): | |
log_name = self._generate_log_name(client_id, resource_id) | |
self._con_pool.setex( | |
name=log_name, | |
time=self._time_bound, | |
value=self._log_value | |
) | |
return True | |
return False | |
def is_allowed(self, client_id: str, resource_id: str) -> bool: | |
num_logs = self.count_logs(client_id, resource_id) | |
return num_logs <= self._number_of_requests | |
def count_logs(self, client_id: str, resource_id: str) -> int: | |
log_pattern = self._get_log_pattern(client_id, resource_id) | |
return len(self._con_pool.keys(log_pattern)) | |
def flush_logs(self, client_id: str, resource_id: str) -> None: | |
log_pattern = self._get_log_pattern(client_id, resource_id) | |
self._con_pool.delete(log_pattern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment