Skip to content

Commit

Permalink
Merge branch 'master' into headers
Browse files Browse the repository at this point in the history
Conflicts:
	tests/test_flask_ext.py
  • Loading branch information
alisaifee committed May 26, 2014
2 parents b4b5f58 + 5827de4 commit d33a5c7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Changelog
=========

0.3.2 2014-05-26
----------------
* Bug fix

* Memory leak when using ``Limiter.storage.MemoryStorage``
(`Issue 4 <https://github.com/alisaifee/flask-limiter/issues/4>`_.)
* Improved test coverage

0.3.1 2014-02-20
----------------
* Strict version requirement on six
Expand Down Expand Up @@ -61,3 +69,4 @@ Changelog




5 changes: 3 additions & 2 deletions flask_limiter/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def __expire_events(self):
for key in self.events:
for event in list(self.events[key]):
with event:
if event.expiry > time.time() and event in self.events[key]:
if event.expiry <= time.time() and event in self.events[key]:
self.events[key].remove(event)
for key in list(self.expirations.keys()):
if self.expirations[key] <= time.time():
self.storage.pop(key)
self.expirations.pop(key)

def __schedule_expiry(self):
if not self.timer.is_alive:
if not self.timer.is_alive():
self.timer = threading.Timer(0.01, self.__expire_events)
self.timer.start()

Expand All @@ -102,6 +102,7 @@ def incr(self, key, expiry, elastic_expiry=False):
window every hit.
"""
self.get(key)
self.__schedule_expiry()
self.storage[key] += 1
if elastic_expiry or self.storage[key] == 1:
self.expirations[key] = time.time() + expiry
Expand Down
11 changes: 11 additions & 0 deletions tests/test_flask_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
from flask import Flask, Blueprint, request, current_app
import hiro
import mock
from flask.ext.limiter.errors import ConfigurationError
from flask.ext.limiter.extension import Limiter


class FlaskExtTests(unittest.TestCase):

def test_invalid_strategy(self):
app = Flask(__name__)
app.config.setdefault("RATELIMIT_STRATEGY", "fubar")
self.assertRaises(ConfigurationError, Limiter, app)

def test_invalid_storage_string(self):
app = Flask(__name__)
app.config.setdefault("RATELIMIT_STORAGE_URL", "fubar://localhost:1234")
self.assertRaises(ConfigurationError, Limiter, app)

def test_combined_rate_limits(self):
app = Flask(__name__)
app.config.setdefault("RATELIMIT_GLOBAL", "1 per hour;10 per day")
Expand Down
18 changes: 16 additions & 2 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def setUp(self):
def test_storage_string(self):
self.assertTrue(isinstance(storage_from_string("memory://"), MemoryStorage))
self.assertTrue(isinstance(storage_from_string("redis://localhost:6379"), RedisStorage))
if get_dependency("memcache"):
self.assertTrue(isinstance(storage_from_string("memcached://localhost:11211"), MemcachedStorage))
self.assertTrue(isinstance(storage_from_string("memcached://localhost:11211"), MemcachedStorage))
self.assertRaises(ConfigurationError, storage_from_string, "blah://")

def test_in_memory(self):
Expand All @@ -50,6 +49,21 @@ def test_in_memory_expiry(self):
time.sleep(0.1)
self.assertTrue(per_min.key_for() not in storage.storage)

def test_in_memory_expiry_moving_window(self):
with hiro.Timeline().freeze() as timeline:
storage = MemoryStorage()
limiter = MovingWindowRateLimiter(storage)
per_min = PER_MINUTE(10)
per_sec = PER_SECOND(1)
for i in range(0,2):
for i in range(0,10):
self.assertTrue(limiter.hit(per_min))
timeline.forward(60)
self.assertTrue(limiter.hit(per_sec))
time.sleep(1)
self.assertEqual([], storage.events[per_min.key_for()])


def test_redis(self):
storage = RedisStorage("redis://localhost:6379")
limiter = FixedWindowRateLimiter(storage)
Expand Down

0 comments on commit d33a5c7

Please sign in to comment.