Python redis.exceptions.WatchError() Examples
The following are 12
code examples of redis.exceptions.WatchError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
redis.exceptions
, or try the search function
.
Example #1
Source File: test_redis_throttled_queue.py From scrapy-cluster with MIT License | 6 votes |
def test_unmoderated(self): # an unmoderated queue is really just testing the number # of hits in a given window self.queue.redis_conn.zcard = MagicMock(return_value=0) self.assertTrue(self.queue.allowed()) self.queue.redis_conn.zcard = MagicMock(return_value=1) self.assertTrue(self.queue.allowed()) self.queue.redis_conn.zcard = MagicMock(return_value=2) self.assertFalse(self.queue.allowed()) # mock exception raised even with good hits self.queue.redis_conn.zcard = MagicMock(return_value=0, side_effect=WatchError) self.assertFalse(self.queue.allowed())
Example #2
Source File: auth.py From Doodle with MIT License | 6 votes |
def generate(cls, next_url): with cls.redis_client.pipeline(transaction=True) as pipe: while True: try: state = cls._generate_state() key = cls.KEY % state pipe.watch(key) if pipe.exists(key): pipe.unwatch(key) continue pipe.multi() pipe.set(key, next_url, ex=CONFIG.AUTH_EXPIRE_TIME) pipe.execute() except WatchError: continue else: return state
Example #3
Source File: lock.py From satori with Apache License 2.0 | 6 votes |
def do_extend(self, additional_time): pipe = self.redis.pipeline() pipe.watch(self.name) lock_value = pipe.get(self.name) if lock_value != self.local.token: raise LockError("Cannot extend a lock that's no longer owned") expiration = pipe.pttl(self.name) if expiration is None or expiration < 0: # Redis evicted the lock key between the previous get() and now # we'll handle this when we call pexpire() expiration = 0 pipe.multi() pipe.pexpire(self.name, expiration + int(additional_time * 1000)) try: response = pipe.execute() except WatchError: # someone else acquired the lock raise LockError("Cannot extend a lock that's no longer owned") if not response[0]: # pexpire returns False if the key doesn't exist raise LockError("Cannot extend a lock that's no longer owned") return True
Example #4
Source File: lock.py From gimel with MIT License | 6 votes |
def do_extend(self, additional_time): pipe = self.redis.pipeline() pipe.watch(self.name) lock_value = pipe.get(self.name) if lock_value != self.local.token: raise LockError("Cannot extend a lock that's no longer owned") expiration = pipe.pttl(self.name) if expiration is None or expiration < 0: # Redis evicted the lock key between the previous get() and now # we'll handle this when we call pexpire() expiration = 0 pipe.multi() pipe.pexpire(self.name, expiration + int(additional_time * 1000)) try: response = pipe.execute() except WatchError: # someone else acquired the lock raise LockError("Cannot extend a lock that's no longer owned") if not response[0]: # pexpire returns False if the key doesn't exist raise LockError("Cannot extend a lock that's no longer owned") return True
Example #5
Source File: lock.py From scalyr-agent-2 with Apache License 2.0 | 6 votes |
def do_extend(self, additional_time): pipe = self.redis.pipeline() pipe.watch(self.name) lock_value = pipe.get(self.name) if lock_value != self.local.token: raise LockError("Cannot extend a lock that's no longer owned") expiration = pipe.pttl(self.name) if expiration is None or expiration < 0: # Redis evicted the lock key between the previous get() and now # we'll handle this when we call pexpire() expiration = 0 pipe.multi() pipe.pexpire(self.name, expiration + int(additional_time * 1000)) try: response = pipe.execute() except WatchError: # someone else acquired the lock raise LockError("Cannot extend a lock that's no longer owned") if not response[0]: # pexpire returns False if the key doesn't exist raise LockError("Cannot extend a lock that's no longer owned") return True
Example #6
Source File: lock.py From revsync with MIT License | 6 votes |
def do_extend(self, additional_time): pipe = self.redis.pipeline() pipe.watch(self.name) lock_value = pipe.get(self.name) if lock_value != self.local.token: raise LockError("Cannot extend a lock that's no longer owned") expiration = pipe.pttl(self.name) if expiration is None or expiration < 0: # Redis evicted the lock key between the previous get() and now # we'll handle this when we call pexpire() expiration = 0 pipe.multi() pipe.pexpire(self.name, expiration + int(additional_time * 1000)) try: response = pipe.execute() except WatchError: # someone else acquired the lock raise LockError("Cannot extend a lock that's no longer owned") if not response[0]: # pexpire returns False if the key doesn't exist raise LockError("Cannot extend a lock that's no longer owned") return True
Example #7
Source File: lock.py From Flask with Apache License 2.0 | 6 votes |
def do_extend(self, additional_time): pipe = self.redis.pipeline() pipe.watch(self.name) lock_value = pipe.get(self.name) if lock_value != self.local.token: raise LockError("Cannot extend a lock that's no longer owned") expiration = pipe.pttl(self.name) if expiration is None or expiration < 0: # Redis evicted the lock key between the previous get() and now # we'll handle this when we call pexpire() expiration = 0 pipe.multi() pipe.pexpire(self.name, expiration + int(additional_time * 1000)) try: response = pipe.execute() except WatchError: # someone else acquired the lock raise LockError("Cannot extend a lock that's no longer owned") if not response[0]: # pexpire returns False if the key doesn't exist raise LockError("Cannot extend a lock that's no longer owned") return True
Example #8
Source File: redis_throttled_queue.py From scrapy-cluster with MIT License | 5 votes |
def test_hits(self): ''' Tests to see if the number of throttle queue hits is within our limit @return: True if the queue was below the limit AND atomically updated ''' with self.redis_conn.pipeline() as pipe: try: pipe.watch(self.window_key) # ---- LOCK value = self.redis_conn.zcard(self.window_key) if value < self.limit: # push value into key now = time.time() pipe.multi() pipe.zadd(self.window_key, now, now) # expire it if it hasnt been touched in a while pipe.expire(self.window_key, int(self.window * 2)) pipe.execute() return True except WatchError: # watch was changed, another thread just messed with the # queue so we can't tell if our result is ok pass return False
Example #9
Source File: test_redis_throttled_queue.py From scrapy-cluster with MIT License | 5 votes |
def test_moderated(self): # a moderated queue should pop ~ every x seconds # we already tested the window limit in the unmoderated test self.queue.is_moderated = MagicMock(return_value=True) self.assertFalse(self.queue.allowed()) self.queue.is_moderated = MagicMock(return_value=False) self.queue.test_hits = MagicMock(return_value=True) self.assertTrue(self.queue.allowed()) # mock exception raised even with good moderation self.queue.test_hits = MagicMock(side_effect=WatchError) self.assertFalse(self.queue.allowed())
Example #10
Source File: auth.py From Doodle with MIT License | 5 votes |
def get(cls, state): # 避免重放攻击,只允许获取一次 with cls.redis_client.pipeline(transaction=True) as pipe: try: key = cls.KEY % state pipe.watch(key) next_url = pipe.get(key) pipe.multi() pipe.delete(key) pipe.execute() except WatchError: return else: return next_url
Example #11
Source File: cache.py From pottery with Apache License 2.0 | 5 votes |
def _retry(self, partial, try_num=0): try: return partial() except WatchError: # pragma: no cover if try_num < self._num_tries - 1: return self._retry(partial, try_num=try_num+1) else: raise
Example #12
Source File: redis_throttled_queue.py From scrapy-cluster with MIT License | 4 votes |
def allowed(self): ''' Check to see if the pop request is allowed @return: True means the maximum was not been reached for the current time window, thus allowing what ever operation follows ''' # Expire old keys (hits) expires = time.time() - self.window self.redis_conn.zremrangebyscore(self.window_key, '-inf', expires) # check if we are hitting too fast for moderation if self.moderation: with self.redis_conn.pipeline() as pipe: try: pipe.watch(self.moderate_key) # ---- LOCK # from this point onward if no errors are raised we # successfully incremented the counter curr_time = time.time() if self.is_moderated(curr_time, pipe) and not \ self.check_elastic(): return False # passed the moderation limit, now check time window # If we have less keys than max, update out moderate key if self.test_hits(): # this is a valid transaction, set the new time pipe.multi() pipe.set(name=self.moderate_key, value=str(curr_time), ex=int(self.window * 2)) pipe.execute() return True except WatchError: # watch was changed, another thread just incremented # the value return False # If we currently have more keys than max, # then limit the action else: return self.test_hits() return False