refactored the session store into a separate class

This commit is contained in:
jamespcole 2015-05-19 19:18:41 +10:00
parent 80f0c42844
commit a8e7903f39

View File

@ -299,7 +299,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
else: else:
if self._session is None and require_auth: if self._session is None and require_auth:
self._session = self.server.sessions.create_session( self._session = self.server.sessions.create(
api_password) api_password)
handle_request_method(self, path_match, data) handle_request_method(self, path_match, data)
@ -429,7 +429,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
session_id = self.get_current_session_id() session_id = self.get_current_session_id()
if session_id is not None: if session_id is not None:
session = self.server.sessions.get_session(session_id) session = self.server.sessions.get(session_id)
if session is not None: if session is not None:
session.reset_expiry() session.reset_expiry()
return session return session
@ -481,7 +481,7 @@ class SessionStore:
self.session_lock = threading.RLock() self.session_lock = threading.RLock()
@Throttle(MIN_SEC_SESSION_CLEARING) @Throttle(MIN_SEC_SESSION_CLEARING)
def remove_expired_sessions(self): def remove_expired(self):
""" Remove any expired sessions. """ """ Remove any expired sessions. """
if self.session_lock.acquire(False): if self.session_lock.acquire(False):
try: try:
@ -496,21 +496,21 @@ class SessionStore:
finally: finally:
self.session_lock.release() self.session_lock.release()
def add_session(self, key, session): def add(self, key, session):
""" Add a new session to the list of tracked sessions """ """ Add a new session to the list of tracked sessions """
self.remove_expired_sessions() self.remove_expired()
with self.session_lock: with self.session_lock:
self._sessions[key] = session self._sessions[key] = session
def get_session(self, key): def get(self, key):
""" get a session by key """ """ get a session by key """
self.remove_expired_sessions() self.remove_expired()
session = self._sessions.get(key, None) session = self._sessions.get(key, None)
if session is not None and session.is_expired: if session is not None and session.is_expired:
return None return None
return session return session
def create_session(self, api_password): def create(self, api_password):
""" Creates a new session and adds it to the sessions """ """ Creates a new session and adds it to the sessions """
if self.enabled is not True: if self.enabled is not True:
return None return None
@ -519,5 +519,5 @@ class SessionStore:
session_id = ''.join([random.choice(chars) for i in range(20)]) session_id = ''.join([random.choice(chars) for i in range(20)])
session = ServerSession(session_id) session = ServerSession(session_id)
session.cookie_values[CONF_API_PASSWORD] = api_password session.cookie_values[CONF_API_PASSWORD] = api_password
self.add_session(session_id, session) self.add(session_id, session)
return session return session