Spaces:
Building
Building
Update session.py
Browse files- session.py +22 -0
session.py
CHANGED
@@ -31,6 +31,13 @@ class Session:
|
|
31 |
# history
|
32 |
self.chat_history: List[Dict[str, str]] = []
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# -------- helper ----------
|
35 |
def add_turn(self, role: str, content: str):
|
36 |
self.chat_history.append({"role": role, "content": content})
|
@@ -66,5 +73,20 @@ class SessionStore:
|
|
66 |
def __contains__(self, sid: str) -> bool:
|
67 |
return self.get_session(sid) is not None
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
session_store = SessionStore()
|
|
|
31 |
# history
|
32 |
self.chat_history: List[Dict[str, str]] = []
|
33 |
|
34 |
+
self.created_at: datetime = datetime.now()
|
35 |
+
self.last_activity: datetime = datetime.now()
|
36 |
+
|
37 |
+
def update_activity(self):
|
38 |
+
"""Update last activity timestamp"""
|
39 |
+
self.last_activity = datetime.now()
|
40 |
+
|
41 |
# -------- helper ----------
|
42 |
def add_turn(self, role: str, content: str):
|
43 |
self.chat_history.append({"role": role, "content": content})
|
|
|
73 |
def __contains__(self, sid: str) -> bool:
|
74 |
return self.get_session(sid) is not None
|
75 |
|
76 |
+
def cleanup_expired_sessions(self, timeout_minutes: int = 30):
|
77 |
+
"""Remove expired sessions"""
|
78 |
+
with self._lock:
|
79 |
+
now = datetime.now()
|
80 |
+
expired = []
|
81 |
+
for sid, session in self._sessions.items():
|
82 |
+
if (now - session.last_activity).total_seconds() > timeout_minutes * 60:
|
83 |
+
expired.append(sid)
|
84 |
+
|
85 |
+
for sid in expired:
|
86 |
+
del self._sessions[sid]
|
87 |
+
log(f"🗑️ Expired session removed: {sid[:8]}...")
|
88 |
+
|
89 |
+
if expired:
|
90 |
+
log(f"📊 Active sessions: {len(self._sessions)}")
|
91 |
|
92 |
session_store = SessionStore()
|