Skip to content

Commit

Permalink
remove auto generated scope names.
Browse files Browse the repository at this point in the history
This doesn't work for multiple app instances running
since each instance will generate a new UUID for the
same shared_limit.
  • Loading branch information
alisaifee committed Jun 24, 2014
1 parent 0065f52 commit c18b670
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 46 deletions.
16 changes: 0 additions & 16 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,6 @@ instance are
def r2():
...
Anonymous shared limit: when no scope is provided a unique id will be used.

.. code-block:: python
shared_limit = limiter.shared_limit("100/hour")
@app.route("..")
@shared_limit
def r1():
...
@app.route("..")
@shared_limit
def r2():
...
Dynamic shared limit: when a callable is passed as scope, the return value
of the function will be used as the scope.
Expand Down
9 changes: 4 additions & 5 deletions flask_limiter/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from functools import wraps
import logging
import uuid

from flask import request, current_app, g

Expand Down Expand Up @@ -161,7 +160,7 @@ def __check_request_limit(self):
def __limit_decorator(self, limit_value,
key_func=None, shared=False,
scope=None):
_scope = scope or uuid.uuid1().hex if shared else None
_scope = scope if shared else None

def _inner(fn):
name = "%s.%s" % (fn.__module__, fn.__name__)
Expand Down Expand Up @@ -200,16 +199,16 @@ def limit(self, limit_value, key_func=None):
return self.__limit_decorator(limit_value, key_func)


def shared_limit(self, limit_value, key_func=None, scope=None):
def shared_limit(self, limit_value, scope, key_func=None):
"""
decorator to be applied to multiple routes sharing the same rate limit.
:param limit_value: rate limit string or a callable that returns a string.
:ref:`ratelimit-string` for more details.
:param scope: a string or callable that returns a string
for defining the rate limiting scope.
:param key_func: function/lambda to extract the unique identifier for
the rate limit. defaults to remote address of the request.
:param scope: a string or callable that returns a string
for defining the rate limiting scope. Defaults to a :func:`uuid.uuid1` value.
"""
return self.__limit_decorator(limit_value, key_func, True, scope)

Expand Down
25 changes: 0 additions & 25 deletions tests/test_flask_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,31 +423,6 @@ def t():
str(int(time.time() + 49))
)

def test_unnamed_shared_limit(self):
app, limiter = self.build_app()
shared_limit_a = limiter.shared_limit("1/minute")
shared_limit_b = limiter.shared_limit("1/minute")
@app.route("/t1")
@shared_limit_a
def route1():
return "route1"

@app.route("/t2")
@shared_limit_a
def route2():
return "route2"

@app.route("/t3")
@shared_limit_b
def route3():
return "route3"

with hiro.Timeline().freeze() as timeline:
with app.test_client() as cli:
self.assertEqual(200, cli.get("/t1").status_code)
self.assertEqual(200, cli.get("/t3").status_code)
self.assertEqual(429, cli.get("/t2").status_code)

def test_named_shared_limit(self):
app, limiter = self.build_app()
shared_limit_a = limiter.shared_limit("1/minute", scope='a')
Expand Down

0 comments on commit c18b670

Please sign in to comment.