cache: allow controlling lock_auto_renewal via .ini config

This commit is contained in:
RhodeCode Admin 2021-07-22 11:47:42 +02:00
parent 42f9103f64
commit fe98575ce6
2 changed files with 16 additions and 8 deletions

View file

@ -391,6 +391,8 @@ rc_cache.cache_perms.expiration_time = 300
; more Redis options: https://dogpilecache.sqlalchemy.org/en/latest/api.html#redis-backends
#rc_cache.cache_perms.arguments.distributed_lock = true
; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen
#rc_cache.cache_perms.arguments.lock_auto_renewal = true
; ***************************************************
; `cache_repo` cache for file tree, Readme, RSS FEEDS
@ -414,6 +416,8 @@ rc_cache.cache_repo.expiration_time = 2592000
; more Redis options: https://dogpilecache.sqlalchemy.org/en/latest/api.html#redis-backends
#rc_cache.cache_repo.arguments.distributed_lock = true
; auto-renew lock to prevent stale locks, slower but safer. Use only if problems happen
#rc_cache.cache_repo.arguments.lock_auto_renewal = true
; ##############
; BEAKER SESSION

View file

@ -224,6 +224,16 @@ class FileNamespaceBackend(PickleSerializer, file_backend.DBMBackend):
class BaseRedisBackend(redis_backend.RedisBackend):
key_prefix = ''
def __init__(self, arguments):
super(BaseRedisBackend, self).__init__(arguments)
self._lock_timeout = self.lock_timeout
self._lock_auto_renewal = arguments.pop("lock_auto_renewal", False)
if self._lock_auto_renewal and not self._lock_timeout:
# set default timeout for auto_renewal
self._lock_timeout = 60
def _create_client(self):
args = {}
@ -289,14 +299,8 @@ class BaseRedisBackend(redis_backend.RedisBackend):
if self.distributed_lock:
lock_key = redis_backend.u('_lock_{0}').format(key)
log.debug('Trying to acquire Redis lock for key %s', lock_key)
auto_renewal = True
lock_timeout = self.lock_timeout
if auto_renewal and not self.lock_timeout:
# set default timeout for auto_renewal
lock_timeout = 10
return get_mutex_lock(self.client, lock_key, lock_timeout,
auto_renewal=auto_renewal)
return get_mutex_lock(self.client, lock_key, self._lock_timeout,
auto_renewal=self._lock_auto_renewal)
else:
return None