From 825206d1a8e9bfba14226d34d46aeb8f11e84837 Mon Sep 17 00:00:00 2001 From: malchu Date: Wed, 10 May 2023 23:03:49 -0400 Subject: [PATCH] added delete_expired_sessions() --- src/flask/sessions.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/flask/sessions.py b/src/flask/sessions.py index e5650d68..254390d7 100644 --- a/src/flask/sessions.py +++ b/src/flask/sessions.py @@ -147,6 +147,9 @@ class SessionInterface: #: this type. null_session_class = NullSession + #: all sessions that are currently open + sessions = [] + #: A flag that indicates if the session interface is pickle based. #: This can be used by Flask extensions to make a decision in regards #: to how to deal with the session object. @@ -269,6 +272,34 @@ class SessionInterface: """ raise NotImplementedError() + def delete_expired_sessions(self, app: Flask) -> int: + """This is called when the sessions need to be refreshed. + + This must return an integer representing the number of sessions + that are still currently running and not yet expired. + + This will return ``None`` to indicate that loading failed in + some way that is not immediately an error. The request + context will fall back to using :meth:`make_null_session` + in this case. + """ + + for session in self.sessions: + number_of_sessions = len(self.sessions) + + if self.get_expiration_time(app, session) < datetime.now(timezone.utc): + self.sessions.remove(session) + + if number_of_sessions == 0: + new_session = self.make_null_session() + self.sessions.append(new_session) + return 1 + + if number_of_sessions == len(self.sessions): + return None + + return len(self.sessions) + session_json_serializer = TaggedJSONSerializer()