-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding session manager to clean expired strays
- Loading branch information
Showing
4 changed files
with
118 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from datetime import datetime, timedelta | ||
from cat.utils import singleton | ||
from cat.log import log | ||
|
||
@singleton | ||
class SessionManager: | ||
""" | ||
This class is responsible for strays session management | ||
- adding new sessions | ||
- getting sessions | ||
- expiring sessions | ||
""" | ||
|
||
def __init__(self, strays: any) -> None: | ||
self.strays = strays | ||
self.sessions = {} | ||
self.user_ids = [] | ||
|
||
def add(self, user_id: str, stray: any, minutes: int = 60 * 24) -> None: | ||
""" | ||
add new session setting expiration time to now plus x minutes | ||
each session uses the expiration time as key and the value is a dict with user_id and stray | ||
""" | ||
|
||
current_time = datetime.now() + timedelta(minutes=minutes) | ||
|
||
# if user_id is already in the session we removed it first | ||
if user_id in self.user_ids: | ||
timestamp_to_remove = [ | ||
timestamp for timestamp in self.sessions if self.sessions[timestamp]["user_id"] == user_id | ||
] | ||
for timestamp in timestamp_to_remove: | ||
self.sessions.pop(timestamp) | ||
|
||
# then we create a new session | ||
self.sessions[current_time] = {"user_id": user_id, "stray": stray} | ||
|
||
# and add the user_id to the list of user_ids | ||
self.user_ids.append(user_id) | ||
pass | ||
|
||
async def evict_expired_sessions(self) -> int: | ||
""" | ||
this method removes expired sessions | ||
""" | ||
|
||
print("active sessions") | ||
print("*" * 20) | ||
for timestamp in self.sessions: | ||
if timestamp >= datetime.now(): | ||
print(f"expiration date: {timestamp} -> {self.sessions[timestamp]}") | ||
print("*" * 20) | ||
|
||
keys_to_remove = [ | ||
timestamp for timestamp in self.sessions if timestamp < datetime.now() | ||
] | ||
for key in keys_to_remove: | ||
user_id = self.sessions[key]["user_id"] | ||
if user_id in self.strays.keys(): | ||
print(f"deleting expired user: {self.strays[user_id]}") | ||
del self.strays[user_id] | ||
if user_id in self.user_ids: | ||
self.user_ids.remove(user_id) | ||
self.sessions.pop(key) | ||
|
||
expired_count = len(keys_to_remove) | ||
if expired_count > 0: | ||
log.info( | ||
f"{expired_count} sessions expired." | ||
) | ||
|
||
return expired_count | ||
|
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