sessions: patch baker to take expire time for redis for auto session cleanup.

This commit is contained in:
Marcin Kuzminski 2020-09-14 11:13:41 +02:00
parent d79287cbfe
commit 5e1a429afd
3 changed files with 46 additions and 0 deletions

View file

@ -0,0 +1,18 @@
diff -rup Beaker-1.9.1-orig/beaker/session.py Beaker-1.9.1/beaker/session.py
--- Beaker-1.9.1-orig/beaker/session.py 2020-04-10 10:23:04.000000000 +0200
+++ Beaker-1.9.1/beaker/session.py 2020-04-10 10:23:34.000000000 +0200
@@ -156,6 +156,14 @@ def __init__(self, request, id=None, invalidate_corrupt=False,
if timeout and not save_accessed_time:
raise BeakerException("timeout requires save_accessed_time")
self.timeout = timeout
+ # We want to pass timeout param to redis backend to support expiration of keys
+ # In future, I believe, we can use this param for memcached and mongo as well
+ if self.timeout is not None and self.type == 'ext:redis':
+ # The backend expiration should always be a bit longer (I decied to use 2 minutes) than the
+ # session expiration itself to prevent the case where the backend data expires while
+ # the session is being read (PR#153)
+ self.namespace_args['timeout'] = self.timeout + 60 * 2
+
self.save_atime = save_accessed_time
self.use_cookies = use_cookies
self.cookie_expires = cookie_expires

View file

@ -0,0 +1,26 @@
diff -rup Beaker-1.9.1-orig/beaker/ext/redisnm.py Beaker-1.9.1/beaker/ext/redisnm.py
--- Beaker-1.9.1-orig/beaker/ext/redisnm.py 2018-04-10 10:23:04.000000000 +0200
+++ Beaker-1.9.1/beaker/ext/redisnm.py 2018-04-10 10:23:34.000000000 +0200
@@ -30,9 +30,10 @@ class RedisNamespaceManager(NamespaceManager):
clients = SyncDict()
- def __init__(self, namespace, url, **kw):
+ def __init__(self, namespace, url, timeout=None, **kw):
super(RedisNamespaceManager, self).__init__(namespace)
self.lock_dir = None # Redis uses redis itself for locking.
+ self.timeout = timeout
if redis is None:
raise RuntimeError('redis is not available')
@@ -68,6 +69,8 @@ def has_key(self, key):
def set_value(self, key, value, expiretime=None):
value = pickle.dumps(value)
+ if expiretime is None and self.timeout is not None:
+ expiretime = self.timeout
if expiretime is not None:
self.client.setex(self._format_key(key), int(expiretime), value)
else:

View file

@ -32,6 +32,8 @@ self: super: {
patches = [
./patches/beaker/patch-beaker-lock-func-debug.diff
./patches/beaker/patch-beaker-metadata-reuse.diff
./patches/beaker/patch-beaker-improved-redis.diff
./patches/beaker/patch-beaker-improved-redis-2.diff
];
});