本文整理汇总了Python中redis.exceptions.WatchError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.WatchError方法的具体用法?Python exceptions.WatchError怎么用?Python exceptions.WatchError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.exceptions
的用法示例。
在下文中一共展示了exceptions.WatchError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unmoderated
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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())
示例2: generate
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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
示例3: do_extend
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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
示例4: test_hits
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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
示例5: test_moderated
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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())
示例6: get
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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
示例7: _retry
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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
示例8: allowed
# 需要导入模块: from redis import exceptions [as 别名]
# 或者: from redis.exceptions import WatchError [as 别名]
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