Skip to content

Commit

Permalink
Add lock timeout for redis storage
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Jun 7, 2015
1 parent 4ea9729 commit b6b755d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion limits/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def acquire_entry(self, key, limit, expiry, no_add=False):
:return: True/False
"""
timestamp = time.time()
with self.lock_impl("%s/LOCK" % key):
with self.lock_impl("%s/LOCK" % key, blocking_timeout=1):
entry = self.storage.lindex(key, limit - 1)
if entry and float(entry) >= timestamp - expiry:
return False
Expand Down
10 changes: 9 additions & 1 deletion tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import hiro
import redis

import redis.lock
from limits.strategies import FixedWindowRateLimiter, MovingWindowRateLimiter
from limits.errors import ConfigurationError
from limits.limits import RateLimitItemPerMinute, RateLimitItemPerSecond
Expand Down Expand Up @@ -153,3 +153,11 @@ def smack(e):
[k.set() for k in events]
time.sleep(2)
self.assertTrue(storage.storage.keys("%s/*" % limit.namespace) == [])

def test_failed_redis_lock(self):
storage = RedisStorage("redis://localhost:6379")
limiter = MovingWindowRateLimiter(storage)
limit = RateLimitItemPerSecond(1000)
key = limit.key_for("test") + "/LOCK"
storage.storage.setnx(key, 1)
self.assertRaises(redis.lock.LockError, limiter.hit, limit, "test")

0 comments on commit b6b755d

Please sign in to comment.