Skip to content

Commit

Permalink
Fix #17: check response from redis and use defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Oct 21, 2014
1 parent 404c9b7 commit 5710268
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 5 additions & 4 deletions flask_limiter/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def get(self, key):
"""
:param str key: the key to get the counter value for
"""
return int(self.storage.get(key))
return int(self.storage.get(key) or 0)

def acquire_entry(self, key, limit, expiry, no_add=False):
"""
Expand Down Expand Up @@ -286,15 +286,16 @@ def get_moving_window(self, key, limit, expiry):
:param int expiry: expiry of entry
"""
timestamp = time.time()
return tuple(self.lua_moving_window(
window = self.lua_moving_window(
[key], [int(timestamp - expiry), limit]
))
)
return window or (timestamp, 0)

def get_expiry(self, key):
"""
:param str key: the key to get the expiry for
"""
return int(self.storage.ttl(key) + time.time())
return int((self.storage.ttl(key) or 0) + time.time())

class MemcachedStorage(Storage):
"""
Expand Down
12 changes: 10 additions & 2 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def t1():
return "t1"

with app.test_client() as cli:
self.assertEqual(200, cli.get("/t1").status_code)
resp = cli.get("/t1")
self.assertEqual(
resp.headers["X-RateLimit-Remaining"],
5
)

def test_redis_request_slower_than_moving_window(self):
app, limiter = self.build_app({
Expand All @@ -51,4 +55,8 @@ def t1():
return "t1"

with app.test_client() as cli:
self.assertEqual(200, cli.get("/t1").status_code)
resp = cli.get("/t1")
self.assertEqual(
resp.headers["X-RateLimit-Remaining"],
5
)

0 comments on commit 5710268

Please sign in to comment.