memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False

This commit is contained in:
souliane 2014-09-22 19:24:07 +02:00
commit 1388ff8fac

View file

@ -40,12 +40,14 @@ class Sessions(object):
"""Sessions are data associated to key used for a temporary moment, with optional profile checking."""
DEFAULT_TIMEOUT = 600
def __init__(self, timeout=None):
def __init__(self, timeout=None, resettable_timeout=True):
"""
@param timeout: nb of seconds before session destruction
@param timeout (int): nb of seconds before session destruction
@param resettable_timeout (bool): if True, the timeout is reset on each access
"""
self._sessions = dict()
self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
self.resettable_timeout = resettable_timeout
def newSession(self, session_data=None, session_id=None, profile=None):
""" Create a new session
@ -83,7 +85,8 @@ class Sessions(object):
raise exceptions.InternalError("You need to use __getitem__ when profile is not set")
if profile_set != profile:
raise exceptions.InternalError("current profile differ from set profile !")
timer.reset(self.timeout)
if self.resettable_timeout:
timer.reset(self.timeout)
return session_data
def __getitem__(self, session_id):
@ -91,7 +94,8 @@ class Sessions(object):
timer, session_data = self._sessions[session_id]
except ValueError:
raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set")
timer.reset(self.timeout)
if self.resettable_timeout:
timer.reset(self.timeout)
return session_data
def __setitem__(self, key, value):
@ -146,7 +150,7 @@ class ProfileSessions(Sessions):
ids = self._profileGetAllIds(profile)
if len(ids) > 1:
raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!')
return self.profileGet(ids[0], profile) if len(ids) == 1 else None
return self.profileGet(ids[0], profile) if len(ids) == 1 else None # XXX: timeout might be reset
def profileDelUnique(self, profile):