Fix locking via RedLock

Fixes #1777
This commit is contained in:
Joseph Schorr 2016-08-29 11:28:53 -04:00
parent 1667fd6612
commit aa7c87d765
2 changed files with 63 additions and 43 deletions

View file

@ -7,6 +7,11 @@ from app import app
logger = logging.getLogger(__name__)
class LockNotAcquiredException(Exception):
""" Exception raised if a GlobalLock could not be acquired. """
class GlobalLock(object):
""" A lock object that blocks globally via Redis. Note that Redis is not considered a tier-1
service, so this lock should not be used for any critical code paths.
@ -20,17 +25,22 @@ class GlobalLock(object):
self._redlock = None
def __enter__(self):
return self.lock()
if not self.acquire():
raise LockNotAcquiredException()
def __exit__(self ,type, value, traceback):
self.unlock()
def __exit__(self, type, value, traceback):
self.release()
def lock(self):
def acquire(self):
logger.debug('Acquiring global lock %s', self._lock_name)
try:
redlock = RedLock(self._lock_name, connection_details=[self._redis_info],
ttl=self._lock_ttl)
redlock.acquire()
ttl=self._lock_ttl)
acquired = redlock.acquire()
if not acquired:
logger.debug('Was unable to not acquire lock %s', self._lock_name)
return False
self._redlock = redlock
logger.debug('Acquired lock %s', self._lock_name)
return True
@ -41,10 +51,15 @@ class GlobalLock(object):
logger.debug('Could not connect to Redis for lock %s: %s', self._lock_name, re)
return False
def unlock(self):
def release(self):
if self._redlock is not None:
logger.debug('Releasing lock %s', self._lock_name)
self._redlock.release()
try:
self._redlock.release()
except RedLockError:
logger.debug('Could not release lock %s', self._lock_name)
except RedisError as re:
logger.debug('Could not connect to Redis for releasing lock %s: %s', self._lock_name, re)
logger.debug('Released lock %s', self._lock_name)
self._redlock = None