def test_context_manager(): """ Test a RedLock can be released by the context manager automically. """ with RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000): lock = RedLock("test_context_manager", [{ "host": "localhost" }], ttl=1000) locked = lock.acquire() assert locked == False lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000) locked = lock.acquire() assert locked == True # try to lock again within a with block try: with RedLock("test_context_manager", [{"host": "localhost"}]): # shouldn't be allowed since someone has the lock already assert False except RedLockError: # we expect this call to error out pass lock.release()
def test_lock_with_multi_backend(): """ Test a RedLock can be acquired when at least N/2+1 redis instances are alive. Set redis instance with port 6380 down or debug sleep during test. """ lock = RedLock("test_simple_lock", connection_details=[{ "host": "localhost", "port": 6379, "db": 0, "socket_timeout": 0.2 }, { "host": "localhost", "port": 6379, "db": 1, "socket_timeout": 0.2 }, { "host": "localhost", "port": 6380, "db": 0, "socket_timeout": 0.2 }], ttl=1000) locked = lock.acquire() lock.release() assert locked == True
def run_test(fixture_id, exc_lock, uut, test, progress, unique_res=None, wait_res=None): """ Temporary test runner :param uut: :param test: :return: """ print (test) report_sequence_status(fixture_id, uut, test, progress) if wait_res: exc_lock.wait_for_all(wait_res) lock = None if unique_res: res_lock = RedLock(unique_res) report_lock_status(fixture_id, unique_res, True) lock = res_lock.acquire() while not lock: time.sleep(0.02) lock = res_lock.acquire() print ('locking {}'.format(unique_res)) time.sleep(randrange(10, 3500) / 1000.0) # todo use REST api to set function status verdict = rand_verdict() payload = {'verdict': verdict, 'test': test, 'uut': uut} # ,'execution_id':execution_id, 'process_id':process_id} print (payload) if lock: print ('releasing {}'.format(unique_res)) res_lock.release() report_lock_status(fixture_id, unique_res, False) return verdict
def test_lock_check(): test_lock = RedLock("test_lock_check") assert test_lock.check() == False test_lock.acquire() assert test_lock.check() == True test_lock.release() assert test_lock.check() == False
def test_context_manager(): """ Test a RedLock can be released by the context manager automically. """ ttl = 1000 with RedLock("test_context_manager", [{"host": "localhost"}], ttl=ttl) as validity: assert 0 < validity < ttl - ttl * CLOCK_DRIFT_FACTOR - 2 lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=ttl) locked = lock.acquire() assert locked is False lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=ttl) locked = lock.acquire() assert locked is True # try to lock again within a with block try: with RedLock("test_context_manager", [{"host": "localhost"}]): # shouldn't be allowed since someone has the lock already assert False except RedLockError: # we expect this call to error out pass lock.release()
def test_context_manager(): """ Test a RedLock can be released by the context manager automically. """ ttl = 1000 with RedLock("test_context_manager", [{ "host": "localhost" }], ttl=ttl) as validity: assert 0 < validity < ttl - ttl * CLOCK_DRIFT_FACTOR - 2 lock = RedLock("test_context_manager", [{ "host": "localhost" }], ttl=ttl) locked = lock.acquire() assert locked is False lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=ttl) locked = lock.acquire() assert locked is True # try to lock again within a with block try: with RedLock("test_context_manager", [{"host": "localhost"}]): # shouldn't be allowed since someone has the lock already assert False except RedLockError: # we expect this call to error out pass lock.release()
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. """ def __init__(self, name, lock_ttl=600): self._lock_name = name self._redis_info = dict(app.config['USER_EVENTS_REDIS']) self._redis_info.update({ 'socket_connect_timeout': 5, 'socket_timeout': 5, 'single_connection_client': True }) self._lock_ttl = lock_ttl self._redlock = None def __enter__(self): if not self.acquire(): raise LockNotAcquiredException() def __exit__(self, type, value, traceback): self.release() def acquire(self): logger.debug('Acquiring global lock %s', self._lock_name) try: self._redlock = RedLock(self._lock_name, connection_details=[self._redis_info], ttl=self._lock_ttl) acquired = self._redlock.acquire() if not acquired: logger.debug('Was unable to not acquire lock %s', self._lock_name) return False logger.debug('Acquired lock %s', self._lock_name) return True except RedLockError: logger.debug('Could not acquire lock %s', self._lock_name) return False except RedisError as re: logger.debug('Could not connect to Redis for lock %s: %s', self._lock_name, re) return False def release(self): if self._redlock is not None: logger.debug('Releasing lock %s', self._lock_name) 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
def test_simple_lock(): """ Test a RedLock can be acquired. """ lock = RedLock("test_simple_lock", [{"host": "localhost"}], ttl=1000) locked = lock.acquire() lock.release() assert locked == True
def test_from_url(): """ Test a RedLock can be acquired via from_url. """ lock = RedLock("test_from_url", [{"url": "redis://localhost/0"}], ttl=1000) locked = lock.acquire() lock.release() assert locked == True
def test_fail_to_lock_acquired(): lock1 = RedLock("test_fail_to_lock_acquired", [{"host": "localhost"}], ttl=1000) lock2 = RedLock("test_fail_to_lock_acquired", [{"host": "localhost"}], ttl=1000) lock1_locked = lock1.acquire() lock2_locked = lock2.acquire() lock1.release() assert lock1_locked == True assert lock2_locked == False
def test_lock_with_validity(): """ Test a RedLock can be acquired and the lock validity is also retruned. """ ttl = 1000 lock = RedLock("test_simple_lock", [{"host": "localhost"}], ttl=ttl) locked, validity = lock.acquire_with_validity() lock.release() assert locked is True assert 0 < validity < ttl - ttl * CLOCK_DRIFT_FACTOR - 2
def test_lock_with_multi_backend(): """ Test a RedLock can be acquired when at least N/2+1 redis instances are alive. Set redis instance with port 6380 down or debug sleep during test. """ lock = RedLock("test_simple_lock", connection_details=[ {"host": "localhost", "port": 6379, "db": 0, "socket_timeout": 0.2}, {"host": "localhost", "port": 6379, "db": 1, "socket_timeout": 0.2}, {"host": "localhost", "port": 6380, "db": 0, "socket_timeout": 0.2}], ttl=1000) locked = lock.acquire() lock.release() assert locked == True
def test_lock_is_locked(): lock = RedLock("test_lock_is_locked") # Clear possible initial states [node.delete(lock.resource) for node in lock.redis_nodes] assert lock.locked() is False lock.acquire() assert lock.locked() is True lock.release() assert lock.locked() is False
def test_locked_span_lock_instances(): lock1 = RedLock("test_locked_span_lock_instances") lock2 = RedLock("test_locked_span_lock_instances") # Clear possible initial states [node.delete(lock1.resource) for node in lock1.redis_nodes] assert lock1.locked() == lock2.locked() is False lock1.acquire() assert lock1.locked() == lock2.locked() is True lock1.release() assert lock1.locked() == lock2.locked() is False
class Lock(): def __init__(self, key, ttl=1000, retry=None, delay=None): self.key = key self.ttl = ttl retry = retry if retry else settings.REDIS_BLOCKER_RETRY delay = delay if delay else settings.REDIS_BLOCKER_DELAY self.redis = RedLock(key, settings.REDIS_BLOCKER, retry, delay, ttl) def __enter__(self): if not self.redis.acquire(): raise RedisLockFail("Can't get block") def __exit__(self, type, value, traceback): self.redis.release()
def test_lock_expire(): lock1 = RedLock("test_lock_expire", [{"host": "localhost"}], ttl=500) lock1.acquire() time.sleep(1) # Now lock1 has expired, we can accquire a lock lock2 = RedLock("test_lock_expire", [{"host": "localhost"}], ttl=1000) locked = lock2.acquire() assert locked == True lock1.release() lock3 = RedLock("test_lock_expire", [{"host": "localhost"}], ttl=1000) locked = lock3.acquire() assert locked == False
def test_context_manager(): """ Test a RedLock can be released by the context manager automically. """ with RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000): lock = RedLock("test_context_manager", [{ "host": "localhost" }], ttl=1000) locked = lock.acquire() assert locked == False lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000) locked = lock.acquire() assert locked == True lock.release()
def get_lock(lockname, timeout=DEFAULT_TIMEOUT, retry_times=1, retry_delay=200): try: from django_redis import get_redis_connection except ImportError: raise Exception("Can't get default Redis connection") else: redis_client = get_redis_connection() if timeout is None: # redlock doesn't support infinite timeout # anyway it's bad idea to have infinite lock timeout = DEFAULT_TIMEOUT lock = RedLock(lockname, [redis_client], retry_times=retry_times, retry_delay=retry_delay, ttl=int(timeout * 1000)) got_lock = lock.acquire() try: yield got_lock finally: if got_lock: lock.release()
def get_long_lock(lockname, retry_times=1, retry_delay=200): try: from django_redis import get_redis_connection except ImportError: raise Exception("Can't get default Redis connection") else: redis_client = get_redis_connection() ttl = int(long_lock_ttl * 1000) lock = RedLock(lockname, [redis_client], retry_times=retry_times, retry_delay=retry_delay, ttl=ttl) got_lock = lock.acquire() if got_lock: thread = LongLockUpdateThread(lock) thread.start() try: yield got_lock finally: if got_lock: lock.release() thread.stop()
def test_context_manager(): """ Test a RedLock can be released by the context manager automically. """ with RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000): lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000) locked = lock.acquire() assert locked == False lock = RedLock("test_context_manager", [{"host": "localhost"}], ttl=1000) locked = lock.acquire() assert locked == True # try to lock again within a with block try: with RedLock("test_context_manager", [{"host": "localhost"}]): # shouldn't be allowed since someone has the lock already assert False except RedLockError: # we expect this call to error out pass lock.release()
def test_lock_release_return(): test_lock = RedLock("test_lock_release_return") test_lock.acquire() assert test_lock.release() == True
elif type == 'remit': return remit_process(command) else: return end_process() lock = RedLock("distributed_lock", connection_details=[ { 'host': '192.168.111.128', 'port': 6379, 'db': 0 }, ]) r = redis.Redis(host='192.168.111.128', port=6379, db=0) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('0.0.0.0', 8888)) s.listen(1) while (1): c, ip = s.accept() print(ip) while 1: command = c.recv(1024) if command: lock.acquire() ret = process_command(command).encode('utf-8') c.sendall(ret) lock.release() else: break