mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 08:47:40 +00:00
Merge pull request #130 from p-tombez/7.0-anon_redis_session
[7.0] Add anonymous redis session expiration configuration
This commit is contained in:
@@ -16,6 +16,8 @@ The storage of sessions in Redis is activated using environment variables.
|
|||||||
* ``ODOO_SESSION_REDIS_PREFIX`` is the prefix for the session keys (optional)
|
* ``ODOO_SESSION_REDIS_PREFIX`` is the prefix for the session keys (optional)
|
||||||
* ``ODOO_SESSION_REDIS_EXPIRATION`` is the time in seconds before expiration of
|
* ``ODOO_SESSION_REDIS_EXPIRATION`` is the time in seconds before expiration of
|
||||||
the sessions (default is 7 days)
|
the sessions (default is 7 days)
|
||||||
|
* ``ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS`` is the time in seconds before expiration of
|
||||||
|
the anonymous sessions (default is 3 hours)
|
||||||
|
|
||||||
|
|
||||||
The keys are set to ``session:<session id>``.
|
The keys are set to ``session:<session id>``.
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ port = int(os.environ.get('ODOO_SESSION_REDIS_PORT', 6379))
|
|||||||
prefix = os.environ.get('ODOO_SESSION_REDIS_PREFIX')
|
prefix = os.environ.get('ODOO_SESSION_REDIS_PREFIX')
|
||||||
password = os.environ.get('ODOO_SESSION_REDIS_PASSWORD')
|
password = os.environ.get('ODOO_SESSION_REDIS_PASSWORD')
|
||||||
expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION')
|
expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION')
|
||||||
|
anon_expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS')
|
||||||
|
|
||||||
|
|
||||||
def session_store():
|
def session_store():
|
||||||
@@ -53,6 +54,7 @@ def session_store():
|
|||||||
redis_client = redis.Redis(host=host, port=port, password=password)
|
redis_client = redis.Redis(host=host, port=port, password=password)
|
||||||
return RedisSessionStore(redis=redis_client, prefix=prefix,
|
return RedisSessionStore(redis=redis_client, prefix=prefix,
|
||||||
expiration=expiration,
|
expiration=expiration,
|
||||||
|
anon_expiration=anon_expiration,
|
||||||
session_class=Session)
|
session_class=Session)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from werkzeug.contrib.sessions import SessionStore
|
|||||||
# this is equal to the duration of the session garbage collector in
|
# this is equal to the duration of the session garbage collector in
|
||||||
# openerp.http.session_gc()
|
# openerp.http.session_gc()
|
||||||
DEFAULT_SESSION_TIMEOUT = 60 * 60 * 24 * 7 # 7 days in seconds
|
DEFAULT_SESSION_TIMEOUT = 60 * 60 * 24 * 7 # 7 days in seconds
|
||||||
|
DEFAULT_SESSION_TIMEOUT_ANONYMOUS = 60 * 60 * 3 # 3 hours in seconds
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -18,13 +19,17 @@ class RedisSessionStore(SessionStore):
|
|||||||
""" SessionStore that saves session to redis """
|
""" SessionStore that saves session to redis """
|
||||||
|
|
||||||
def __init__(self, redis, session_class=None,
|
def __init__(self, redis, session_class=None,
|
||||||
prefix='', expiration=None):
|
prefix='', expiration=None, anon_expiration=None):
|
||||||
super(RedisSessionStore, self).__init__(session_class=session_class)
|
super(RedisSessionStore, self).__init__(session_class=session_class)
|
||||||
self.redis = redis
|
self.redis = redis
|
||||||
if expiration is None:
|
if expiration is None:
|
||||||
self.expiration = DEFAULT_SESSION_TIMEOUT
|
self.expiration = DEFAULT_SESSION_TIMEOUT
|
||||||
else:
|
else:
|
||||||
self.expiration = expiration
|
self.expiration = expiration
|
||||||
|
if anon_expiration is None:
|
||||||
|
self.anon_expiration = DEFAULT_SESSION_TIMEOUT_ANONYMOUS
|
||||||
|
else:
|
||||||
|
self.anon_expiration = anon_expiration
|
||||||
self.prefix = u'session:'
|
self.prefix = u'session:'
|
||||||
if prefix:
|
if prefix:
|
||||||
self.prefix = u'%s:%s:' % (
|
self.prefix = u'%s:%s:' % (
|
||||||
@@ -43,8 +48,15 @@ class RedisSessionStore(SessionStore):
|
|||||||
# That's why we need to look for the first item in it to get the
|
# That's why we need to look for the first item in it to get the
|
||||||
# expiration parameter
|
# expiration parameter
|
||||||
session_oe = session.itervalues().next()
|
session_oe = session.itervalues().next()
|
||||||
expiration = getattr(session_oe, 'expiration', self.expiration)
|
|
||||||
|
|
||||||
|
# allow to set a custom expiration for a session
|
||||||
|
# such as a very short one for monitoring requests
|
||||||
|
if session._uid:
|
||||||
|
expiration = getattr(session_oe, 'expiration', self.expiration)
|
||||||
|
else:
|
||||||
|
expiration = getattr(
|
||||||
|
session_oe, 'expiration', self.anon_expiration
|
||||||
|
)
|
||||||
if _logger.isEnabledFor(logging.DEBUG):
|
if _logger.isEnabledFor(logging.DEBUG):
|
||||||
_logger.debug("saving session with key '%s' and "
|
_logger.debug("saving session with key '%s' and "
|
||||||
"expiration of %s seconds",
|
"expiration of %s seconds",
|
||||||
|
|||||||
Reference in New Issue
Block a user