quart-rate-limiter 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ 0.1.0 2019-09-15
2
+ ----------------
3
+
4
+ * Released initial alpha version.
@@ -0,0 +1,22 @@
1
+ Copyright P G Jones 2019.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ include CHANGELOG.rst
2
+ include LICENSE
3
+ include pyproject.toml
4
+ include quart_rate_limiter/py.typed
5
+ include README.rst
6
+ include setup.cfg
7
+ recursive-include quart_limiter *.py
8
+ recursive-include tests *.py
9
+ exclude .gitlab-ci.yml
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.1
2
+ Name: Quart-Rate-Limiter
3
+ Version: 0.1.0
4
+ Summary: A Quart extension to provide rate limiting support.
5
+ Home-page: https://gitlab.com/pgjones/quart-rate-limiter/
6
+ Author: P G Jones
7
+ Author-email: philip.graham.jones@googlemail.com
8
+ License: MIT
9
+ Description: Quart-Rate-Limiter
10
+ ==================
11
+
12
+ |Build Status| |pypi| |python| |license|
13
+
14
+ Quart-Rate-Limiter is an extension for `Quart
15
+ <https://gitlab.com/pgjones/quart>`_ to allow for rate limits to be
16
+ defined and enforced on a per route basis. The 429 error response
17
+ includes a `RFC7231
18
+ <https://tools.ietf.org/html/rfc7231#section-7.1.3>`_ compliant
19
+ ``Retry-After`` header and the successful responses contain headers
20
+ compliant with the `RateLimit Header Fields for HTTP
21
+ <https://tools.ietf.org/html/draft-polli-ratelimit-headers-00>`_ RFC
22
+ draft.
23
+
24
+ Usage
25
+ -----
26
+
27
+ To add a rate limit first initialise the RateLimiting extension with
28
+ the application,
29
+
30
+ .. code-block:: python
31
+
32
+ app = Quart(__name__)
33
+ rate_limiter = RateLimiter(app)
34
+
35
+ or via the factory pattern,
36
+
37
+ .. code-block:: python
38
+
39
+ rate_limiter = RateLimiter()
40
+
41
+ def create_app():
42
+ app = Quart(__name__)
43
+ rate_limiter.init_app(app)
44
+ return app
45
+
46
+ Now this is done you can apply rate limits to any route by using the
47
+ ``rate_limit`` decorator,
48
+
49
+ .. code-block:: python
50
+
51
+ @app.route('/')
52
+ @rate_limit(1, timedelta(seconds=10))
53
+ async def handler():
54
+ ...
55
+
56
+ To alter the identification of remote users you can either supply a
57
+ global key function when initialising the extension, or on a per route
58
+ basis.
59
+
60
+ By default rate limiting information (TATs) will be stored in memory,
61
+ which will result in unexpected behaviour if multiple workers are
62
+ used. To solve this a redis store can be used by installing the
63
+ ``redis`` extra (``pip install quart-rate-limiter[redis]``) and then
64
+ using as so,
65
+
66
+ .. code-block:: python
67
+
68
+ from quart_rate_limiter.redis_store import RedisStore
69
+
70
+ redis_store = RedisStore(address)
71
+ RateLimiter(app, store=redis_store)
72
+
73
+ This store uses `aioredis <https://github.com/aio-libs/aioredis>`_,
74
+ and any extra keyword arguments passed to the ``RedisStore``
75
+ constructor will be passed to the aioredis ``create_redis`` function.
76
+
77
+ A custom store is possible, see the ``RateLimiterStoreABC`` for the
78
+ required interface.
79
+
80
+ Simple examples
81
+ ~~~~~~~~~~~~~~~
82
+
83
+ To limit a route to 1 request per second and a maximum of 20 per minute,
84
+
85
+ .. code-block:: python
86
+
87
+ @app.route('/')
88
+ @rate_limit(1, timedelta(seconds=1))
89
+ @rate_limit(20, timedelta(minutes=1))
90
+ async def handler():
91
+ ...
92
+
93
+ To identify remote users based on the forwarded IP, rather than the
94
+ direct IP (if behind a load balancer),
95
+
96
+ .. code-block:: python
97
+
98
+ async def key_function():
99
+ # Return the X-Forwarded-For as the user-agent identifier,
100
+ # unless it isn't present (direct connection).
101
+ return request.headers.get("X-Forwarded-For", request.remote_addr)
102
+
103
+ RateLimiter(app, key_function=key_function)
104
+
105
+ The ``key_function`` is a coroutine function to allow session lookups
106
+ if appropriate.
107
+
108
+ Contributing
109
+ ------------
110
+
111
+ Quart-Rate-Limiter is developed on `GitLab
112
+ <https://gitlab.com/pgjones/quart-rate-limiter>`_. You are very welcome to
113
+ open `issues <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_ or
114
+ propose `merge requests
115
+ <https://gitlab.com/pgjones/quart-rate-limiter/merge_requests>`_.
116
+
117
+ Testing
118
+ ~~~~~~~
119
+
120
+ The best way to test Quart-Rate-Limiter is with Tox,
121
+
122
+ .. code-block:: console
123
+
124
+ $ pip install tox
125
+ $ tox
126
+
127
+ this will check the code style and run the tests.
128
+
129
+ Help
130
+ ----
131
+
132
+ This README is the best place to start, after that try opening an
133
+ `issue <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_.
134
+
135
+
136
+ .. |Build Status| image:: https://gitlab.com/pgjones/quart-rate-limiter/badges/master/build.svg
137
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/commits/master
138
+
139
+ .. |pypi| image:: https://img.shields.io/pypi/v/quart-rate-limiter.svg
140
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
141
+
142
+ .. |python| image:: https://img.shields.io/pypi/pyversions/quart-rate-limiter.svg
143
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
144
+
145
+ .. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg
146
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/blob/master/LICENSE
147
+
148
+ Platform: UNKNOWN
149
+ Classifier: Development Status :: 3 - Alpha
150
+ Classifier: Environment :: Web Environment
151
+ Classifier: Intended Audience :: Developers
152
+ Classifier: License :: OSI Approved :: MIT License
153
+ Classifier: Operating System :: OS Independent
154
+ Classifier: Programming Language :: Python
155
+ Classifier: Programming Language :: Python :: 3
156
+ Classifier: Programming Language :: Python :: 3.7
157
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
158
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
159
+ Requires-Python: >=3.7.0
160
+ Provides-Extra: redis
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.1
2
+ Name: Quart-Rate-Limiter
3
+ Version: 0.1.0
4
+ Summary: A Quart extension to provide rate limiting support.
5
+ Home-page: https://gitlab.com/pgjones/quart-rate-limiter/
6
+ Author: P G Jones
7
+ Author-email: philip.graham.jones@googlemail.com
8
+ License: MIT
9
+ Description: Quart-Rate-Limiter
10
+ ==================
11
+
12
+ |Build Status| |pypi| |python| |license|
13
+
14
+ Quart-Rate-Limiter is an extension for `Quart
15
+ <https://gitlab.com/pgjones/quart>`_ to allow for rate limits to be
16
+ defined and enforced on a per route basis. The 429 error response
17
+ includes a `RFC7231
18
+ <https://tools.ietf.org/html/rfc7231#section-7.1.3>`_ compliant
19
+ ``Retry-After`` header and the successful responses contain headers
20
+ compliant with the `RateLimit Header Fields for HTTP
21
+ <https://tools.ietf.org/html/draft-polli-ratelimit-headers-00>`_ RFC
22
+ draft.
23
+
24
+ Usage
25
+ -----
26
+
27
+ To add a rate limit first initialise the RateLimiting extension with
28
+ the application,
29
+
30
+ .. code-block:: python
31
+
32
+ app = Quart(__name__)
33
+ rate_limiter = RateLimiter(app)
34
+
35
+ or via the factory pattern,
36
+
37
+ .. code-block:: python
38
+
39
+ rate_limiter = RateLimiter()
40
+
41
+ def create_app():
42
+ app = Quart(__name__)
43
+ rate_limiter.init_app(app)
44
+ return app
45
+
46
+ Now this is done you can apply rate limits to any route by using the
47
+ ``rate_limit`` decorator,
48
+
49
+ .. code-block:: python
50
+
51
+ @app.route('/')
52
+ @rate_limit(1, timedelta(seconds=10))
53
+ async def handler():
54
+ ...
55
+
56
+ To alter the identification of remote users you can either supply a
57
+ global key function when initialising the extension, or on a per route
58
+ basis.
59
+
60
+ By default rate limiting information (TATs) will be stored in memory,
61
+ which will result in unexpected behaviour if multiple workers are
62
+ used. To solve this a redis store can be used by installing the
63
+ ``redis`` extra (``pip install quart-rate-limiter[redis]``) and then
64
+ using as so,
65
+
66
+ .. code-block:: python
67
+
68
+ from quart_rate_limiter.redis_store import RedisStore
69
+
70
+ redis_store = RedisStore(address)
71
+ RateLimiter(app, store=redis_store)
72
+
73
+ This store uses `aioredis <https://github.com/aio-libs/aioredis>`_,
74
+ and any extra keyword arguments passed to the ``RedisStore``
75
+ constructor will be passed to the aioredis ``create_redis`` function.
76
+
77
+ A custom store is possible, see the ``RateLimiterStoreABC`` for the
78
+ required interface.
79
+
80
+ Simple examples
81
+ ~~~~~~~~~~~~~~~
82
+
83
+ To limit a route to 1 request per second and a maximum of 20 per minute,
84
+
85
+ .. code-block:: python
86
+
87
+ @app.route('/')
88
+ @rate_limit(1, timedelta(seconds=1))
89
+ @rate_limit(20, timedelta(minutes=1))
90
+ async def handler():
91
+ ...
92
+
93
+ To identify remote users based on the forwarded IP, rather than the
94
+ direct IP (if behind a load balancer),
95
+
96
+ .. code-block:: python
97
+
98
+ async def key_function():
99
+ # Return the X-Forwarded-For as the user-agent identifier,
100
+ # unless it isn't present (direct connection).
101
+ return request.headers.get("X-Forwarded-For", request.remote_addr)
102
+
103
+ RateLimiter(app, key_function=key_function)
104
+
105
+ The ``key_function`` is a coroutine function to allow session lookups
106
+ if appropriate.
107
+
108
+ Contributing
109
+ ------------
110
+
111
+ Quart-Rate-Limiter is developed on `GitLab
112
+ <https://gitlab.com/pgjones/quart-rate-limiter>`_. You are very welcome to
113
+ open `issues <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_ or
114
+ propose `merge requests
115
+ <https://gitlab.com/pgjones/quart-rate-limiter/merge_requests>`_.
116
+
117
+ Testing
118
+ ~~~~~~~
119
+
120
+ The best way to test Quart-Rate-Limiter is with Tox,
121
+
122
+ .. code-block:: console
123
+
124
+ $ pip install tox
125
+ $ tox
126
+
127
+ this will check the code style and run the tests.
128
+
129
+ Help
130
+ ----
131
+
132
+ This README is the best place to start, after that try opening an
133
+ `issue <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_.
134
+
135
+
136
+ .. |Build Status| image:: https://gitlab.com/pgjones/quart-rate-limiter/badges/master/build.svg
137
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/commits/master
138
+
139
+ .. |pypi| image:: https://img.shields.io/pypi/v/quart-rate-limiter.svg
140
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
141
+
142
+ .. |python| image:: https://img.shields.io/pypi/pyversions/quart-rate-limiter.svg
143
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
144
+
145
+ .. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg
146
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/blob/master/LICENSE
147
+
148
+ Platform: UNKNOWN
149
+ Classifier: Development Status :: 3 - Alpha
150
+ Classifier: Environment :: Web Environment
151
+ Classifier: Intended Audience :: Developers
152
+ Classifier: License :: OSI Approved :: MIT License
153
+ Classifier: Operating System :: OS Independent
154
+ Classifier: Programming Language :: Python
155
+ Classifier: Programming Language :: Python :: 3
156
+ Classifier: Programming Language :: Python :: 3.7
157
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
158
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
159
+ Requires-Python: >=3.7.0
160
+ Provides-Extra: redis
@@ -0,0 +1,17 @@
1
+ CHANGELOG.rst
2
+ LICENSE
3
+ MANIFEST.in
4
+ README.rst
5
+ pyproject.toml
6
+ setup.cfg
7
+ setup.py
8
+ Quart_Rate_Limiter.egg-info/PKG-INFO
9
+ Quart_Rate_Limiter.egg-info/SOURCES.txt
10
+ Quart_Rate_Limiter.egg-info/dependency_links.txt
11
+ Quart_Rate_Limiter.egg-info/requires.txt
12
+ Quart_Rate_Limiter.egg-info/top_level.txt
13
+ quart_rate_limiter/__init__.py
14
+ quart_rate_limiter/py.typed
15
+ quart_rate_limiter/redis_store.py
16
+ quart_rate_limiter/store.py
17
+ tests/test_basic.py
@@ -0,0 +1,4 @@
1
+ Quart>=0.7
2
+
3
+ [redis]
4
+ aioredis
@@ -0,0 +1 @@
1
+ quart_rate_limiter
@@ -0,0 +1,138 @@
1
+ Quart-Rate-Limiter
2
+ ==================
3
+
4
+ |Build Status| |pypi| |python| |license|
5
+
6
+ Quart-Rate-Limiter is an extension for `Quart
7
+ <https://gitlab.com/pgjones/quart>`_ to allow for rate limits to be
8
+ defined and enforced on a per route basis. The 429 error response
9
+ includes a `RFC7231
10
+ <https://tools.ietf.org/html/rfc7231#section-7.1.3>`_ compliant
11
+ ``Retry-After`` header and the successful responses contain headers
12
+ compliant with the `RateLimit Header Fields for HTTP
13
+ <https://tools.ietf.org/html/draft-polli-ratelimit-headers-00>`_ RFC
14
+ draft.
15
+
16
+ Usage
17
+ -----
18
+
19
+ To add a rate limit first initialise the RateLimiting extension with
20
+ the application,
21
+
22
+ .. code-block:: python
23
+
24
+ app = Quart(__name__)
25
+ rate_limiter = RateLimiter(app)
26
+
27
+ or via the factory pattern,
28
+
29
+ .. code-block:: python
30
+
31
+ rate_limiter = RateLimiter()
32
+
33
+ def create_app():
34
+ app = Quart(__name__)
35
+ rate_limiter.init_app(app)
36
+ return app
37
+
38
+ Now this is done you can apply rate limits to any route by using the
39
+ ``rate_limit`` decorator,
40
+
41
+ .. code-block:: python
42
+
43
+ @app.route('/')
44
+ @rate_limit(1, timedelta(seconds=10))
45
+ async def handler():
46
+ ...
47
+
48
+ To alter the identification of remote users you can either supply a
49
+ global key function when initialising the extension, or on a per route
50
+ basis.
51
+
52
+ By default rate limiting information (TATs) will be stored in memory,
53
+ which will result in unexpected behaviour if multiple workers are
54
+ used. To solve this a redis store can be used by installing the
55
+ ``redis`` extra (``pip install quart-rate-limiter[redis]``) and then
56
+ using as so,
57
+
58
+ .. code-block:: python
59
+
60
+ from quart_rate_limiter.redis_store import RedisStore
61
+
62
+ redis_store = RedisStore(address)
63
+ RateLimiter(app, store=redis_store)
64
+
65
+ This store uses `aioredis <https://github.com/aio-libs/aioredis>`_,
66
+ and any extra keyword arguments passed to the ``RedisStore``
67
+ constructor will be passed to the aioredis ``create_redis`` function.
68
+
69
+ A custom store is possible, see the ``RateLimiterStoreABC`` for the
70
+ required interface.
71
+
72
+ Simple examples
73
+ ~~~~~~~~~~~~~~~
74
+
75
+ To limit a route to 1 request per second and a maximum of 20 per minute,
76
+
77
+ .. code-block:: python
78
+
79
+ @app.route('/')
80
+ @rate_limit(1, timedelta(seconds=1))
81
+ @rate_limit(20, timedelta(minutes=1))
82
+ async def handler():
83
+ ...
84
+
85
+ To identify remote users based on the forwarded IP, rather than the
86
+ direct IP (if behind a load balancer),
87
+
88
+ .. code-block:: python
89
+
90
+ async def key_function():
91
+ # Return the X-Forwarded-For as the user-agent identifier,
92
+ # unless it isn't present (direct connection).
93
+ return request.headers.get("X-Forwarded-For", request.remote_addr)
94
+
95
+ RateLimiter(app, key_function=key_function)
96
+
97
+ The ``key_function`` is a coroutine function to allow session lookups
98
+ if appropriate.
99
+
100
+ Contributing
101
+ ------------
102
+
103
+ Quart-Rate-Limiter is developed on `GitLab
104
+ <https://gitlab.com/pgjones/quart-rate-limiter>`_. You are very welcome to
105
+ open `issues <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_ or
106
+ propose `merge requests
107
+ <https://gitlab.com/pgjones/quart-rate-limiter/merge_requests>`_.
108
+
109
+ Testing
110
+ ~~~~~~~
111
+
112
+ The best way to test Quart-Rate-Limiter is with Tox,
113
+
114
+ .. code-block:: console
115
+
116
+ $ pip install tox
117
+ $ tox
118
+
119
+ this will check the code style and run the tests.
120
+
121
+ Help
122
+ ----
123
+
124
+ This README is the best place to start, after that try opening an
125
+ `issue <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_.
126
+
127
+
128
+ .. |Build Status| image:: https://gitlab.com/pgjones/quart-rate-limiter/badges/master/build.svg
129
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/commits/master
130
+
131
+ .. |pypi| image:: https://img.shields.io/pypi/v/quart-rate-limiter.svg
132
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
133
+
134
+ .. |python| image:: https://img.shields.io/pypi/pyversions/quart-rate-limiter.svg
135
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
136
+
137
+ .. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg
138
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/blob/master/LICENSE
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+
4
+ [tool.black]
5
+ line-length = 100
6
+ target-version = ["py37"]
7
+
8
+ [tool.isort]
9
+ dont_skip = ["__init__.py"]
10
+ combine_as_imports = true
11
+ force_grid_wrap = 0
12
+ include_trailing_comma = true
13
+ known_first_party = "quart_rate_limiter, tests"
14
+ known_third_party = "_pytest, aioredis, pytest, quart"
15
+ line_length = 100
16
+ multi_line_output = 3
17
+ no_lines_before = "LOCALFOLDER"
18
+ order_by_type = false
19
+ reverse_relative = true
@@ -0,0 +1,204 @@
1
+ from dataclasses import dataclass
2
+ from datetime import datetime, timedelta
3
+ from typing import Awaitable, Callable, List, Optional
4
+
5
+ from quart import current_app, Quart, request, Response
6
+ from quart.exceptions import HTTPException
7
+
8
+ from .store import MemoryStore, RateLimiterStoreABC
9
+
10
+ QUART_RATE_LIMITER_ATTRIBUTE = "_quart_rate_limiter_limits"
11
+
12
+ KeyCallable = Callable[[], Awaitable[str]]
13
+
14
+
15
+ class RateLimitExceeded(HTTPException):
16
+ """A 429 Rate limit exceeded error.
17
+
18
+ Arguments:
19
+ retry_after: Seconds left till the remaining resets to the limit.
20
+ """
21
+
22
+ def __init__(self, retry_after: int) -> None:
23
+ super().__init__(429, "Rate Limit Exceeded", "RATE_LIMIT_EXCEEDED")
24
+ self.retry_after = retry_after
25
+
26
+ def get_headers(self) -> dict:
27
+ return {"Retry-After": str(self.retry_after)}
28
+
29
+
30
+ @dataclass
31
+ class RateLimit:
32
+ count: int
33
+ period: timedelta
34
+ key_function: Optional[KeyCallable]
35
+
36
+ @property
37
+ def inverse(self) -> float:
38
+ return self.period.total_seconds() / self.count
39
+
40
+ @property
41
+ def key(self) -> str:
42
+ return f"{self.count}-{self.period.total_seconds()}"
43
+
44
+
45
+ def rate_limit(
46
+ limit: int, period: timedelta, key_function: Optional[KeyCallable] = None
47
+ ) -> Callable:
48
+ """A decorator to add a rate limit marker to the route.
49
+
50
+ This should be used to wrap a route handler (or view function) to
51
+ apply a rate limit to requests to that route. Note that it is
52
+ important that this decorator be wrapped by the route decorator
53
+ and not vice, versa, as below.
54
+
55
+ .. code-block:: python
56
+
57
+ @app.route('/')
58
+ @rate_limit(10, timedelta(seconds=10))
59
+ async def index():
60
+ ...
61
+
62
+ Arguments:
63
+ limit: The maximum number of requests to serve within a
64
+ period.
65
+ period: The duration over which the number of requests must
66
+ not exceed the *limit*.
67
+ key_function: A coroutine function that returns a unique key
68
+ to identify the user agent.
69
+
70
+ .. code-block:: python
71
+
72
+ async def example_key_function() -> str:
73
+ return request.remote_addr
74
+
75
+ """
76
+
77
+ def decorator(func: Callable) -> Callable:
78
+ rate_limits = getattr(func, QUART_RATE_LIMITER_ATTRIBUTE, [])
79
+ rate_limits.append(RateLimit(limit, period, key_function))
80
+ setattr(func, QUART_RATE_LIMITER_ATTRIBUTE, rate_limits)
81
+ return func
82
+
83
+ return decorator
84
+
85
+
86
+ async def remote_addr_key() -> str:
87
+ return request.remote_addr
88
+
89
+
90
+ class RateLimiter:
91
+ """A Rate limiter instance.
92
+
93
+ This can be used to initialise Rate Limiting for a given app,
94
+ either directly,
95
+
96
+ .. code-block:: python
97
+
98
+ app = Quart(__name__)
99
+ rate_limiter = RateLimiter(app)
100
+
101
+ or via the factory pattern,
102
+
103
+ .. code-block:: python
104
+
105
+ rate_limiter = RateLimiter()
106
+
107
+ def create_app():
108
+ app = Quart(__name__)
109
+ rate_limiter.init_app(app)
110
+ return app
111
+
112
+ The limiter itself can be customised using the following
113
+ arguments,
114
+
115
+ Arguments:
116
+ key_function: A coroutine function that returns a unique key
117
+ to identify the user agent.
118
+ store: The store that contains the theoretical arrival times by
119
+ key.
120
+ """
121
+
122
+ def __init__(
123
+ self,
124
+ app: Optional[Quart] = None,
125
+ key_function: KeyCallable = remote_addr_key,
126
+ store: Optional[RateLimiterStoreABC] = None,
127
+ ) -> None:
128
+ self.key_function = key_function
129
+ self.store: RateLimiterStoreABC
130
+ if store is None:
131
+ self.store = MemoryStore()
132
+ else:
133
+ self.store = store
134
+
135
+ if app is not None:
136
+ self.init_app(app)
137
+
138
+ def init_app(self, app: Quart) -> None:
139
+ app.before_request(self._before_request)
140
+ app.after_request(self._after_request)
141
+ app.before_serving(self._before_serving)
142
+ app.after_serving(self._after_serving)
143
+
144
+ async def _before_serving(self) -> None:
145
+ await self.store.before_serving()
146
+
147
+ async def _after_serving(self) -> None:
148
+ await self.store.after_serving()
149
+
150
+ async def _before_request(self) -> None:
151
+ endpoint = request.endpoint
152
+ view_func = current_app.view_functions.get(endpoint)
153
+ if view_func is not None:
154
+ rate_limits: List[RateLimit] = getattr(view_func, QUART_RATE_LIMITER_ATTRIBUTE, [])
155
+ await self._raise_on_rejection(endpoint, rate_limits)
156
+ await self._update_limits(endpoint, rate_limits)
157
+
158
+ async def _raise_on_rejection(self, endpoint: str, rate_limits: List[RateLimit]) -> None:
159
+ now = datetime.utcnow()
160
+ for rate_limit in rate_limits:
161
+ key = await self._create_key(endpoint, rate_limit)
162
+ # This is the GCRA rate limiting system and tat stands for
163
+ # the theoretical arrival time.
164
+ tat = max(await self.store.get(key, now), now)
165
+ separation = (tat - now).total_seconds()
166
+ max_interval = rate_limit.period.total_seconds() - rate_limit.inverse
167
+ if separation > max_interval:
168
+ retry_after = ((tat - timedelta(seconds=max_interval)) - now).total_seconds()
169
+ raise RateLimitExceeded(int(retry_after))
170
+
171
+ async def _update_limits(self, endpoint: str, rate_limits: List[RateLimit]) -> None:
172
+ # Update the tats for all the rate limits. This must only
173
+ # occur if no limit rejects the request.
174
+ now = datetime.utcnow()
175
+ for rate_limit in rate_limits:
176
+ key = await self._create_key(endpoint, rate_limit)
177
+ tat = max(await self.store.get(key, now), now)
178
+ new_tat = max(tat, now) + timedelta(seconds=rate_limit.inverse)
179
+ await self.store.set(key, new_tat)
180
+
181
+ async def _after_request(self, response: Response) -> Response:
182
+ endpoint = request.endpoint
183
+ view_func = current_app.view_functions.get(endpoint)
184
+ rate_limits: List[RateLimit] = getattr(view_func, QUART_RATE_LIMITER_ATTRIBUTE, [])
185
+ try:
186
+ min_limit = min(rate_limits, key=lambda rate_limit: rate_limit.period.total_seconds())
187
+ except ValueError:
188
+ pass # No rate limits
189
+ else:
190
+ key = await self._create_key(endpoint, min_limit)
191
+ now = datetime.utcnow()
192
+ tat = max(await self.store.get(key, now), now)
193
+ separation = (tat - now).total_seconds()
194
+ remaining = int((min_limit.period.total_seconds() - separation) / min_limit.inverse)
195
+ response.headers["RateLimit-Limit"] = str(min_limit.count)
196
+ response.headers["RateLimit-Remaining"] = str(remaining)
197
+ response.headers["RateLimit-Reset"] = str(int(separation))
198
+ return response
199
+
200
+ async def _create_key(self, endpoint: str, rate_limit: RateLimit) -> str:
201
+ key_function = rate_limit.key_function or self.key_function
202
+ key = await key_function()
203
+ app_name = current_app.import_name
204
+ return f"{app_name}-{endpoint}-{rate_limit.key}-{key}"
@@ -0,0 +1 @@
1
+ Marker
@@ -0,0 +1,38 @@
1
+ from datetime import datetime
2
+ from typing import Any, Optional
3
+
4
+ from aioredis import create_redis, Redis
5
+
6
+ from .store import RateLimiterStoreABC
7
+
8
+
9
+ class RedisStore(RateLimiterStoreABC):
10
+ """An redis backed store of rate limits.
11
+
12
+ Arguments:
13
+ address: The address of the redis instance.
14
+ kwargs: Any keyword arguments to pass to the redis client on
15
+ creation, see the aioredis documentation.
16
+ """
17
+
18
+ def __init__(self, address: str, **kwargs: Any) -> None:
19
+ self._redis: Optional[Redis] = None
20
+ self._redis_arguments = (address, kwargs)
21
+
22
+ async def before_serving(self) -> None:
23
+ self._redis = await create_redis(self._redis_arguments[0], **self._redis_arguments[1])
24
+
25
+ async def get(self, key: str, default: datetime) -> datetime:
26
+ result = await self._redis.get(key)
27
+ if result is None:
28
+ return default
29
+ else:
30
+ return result
31
+
32
+ async def set(self, key: str, tat: datetime) -> None:
33
+ await self._redis.set(key, tat)
34
+
35
+ async def after_serving(self) -> None:
36
+ self._redis.close()
37
+ await self._redis.wait_closed()
38
+ self._redis = None
@@ -0,0 +1,58 @@
1
+ from abc import ABCMeta, abstractmethod
2
+ from datetime import datetime
3
+ from typing import Dict
4
+
5
+
6
+ class RateLimiterStoreABC(metaclass=ABCMeta):
7
+ @abstractmethod
8
+ async def before_serving(self) -> None:
9
+ """A coroutine within which any setup can be done."""
10
+ pass
11
+
12
+ @abstractmethod
13
+ async def get(self, key: str, default: datetime) -> datetime:
14
+ """Get the TAT for the given *key* if present or the *default* if not.
15
+
16
+ Arguments:
17
+ key: The key to indentify the TAT.
18
+ default: If no TAT for the *key* is available, return this
19
+ default.
20
+
21
+ Returns:
22
+ A Theoretical Arrival Time, TAT.
23
+ """
24
+ pass
25
+
26
+ @abstractmethod
27
+ async def set(self, key: str, tat: datetime) -> None:
28
+ """Set the TAT for the given *key*.
29
+
30
+ Arguments:
31
+ key: The key to indentify the TAT.
32
+ tat: The TAT value to set.
33
+ """
34
+ pass
35
+
36
+ @abstractmethod
37
+ async def after_serving(self) -> None:
38
+ """A coroutine within which any cleanup can be done."""
39
+ pass
40
+
41
+
42
+ class MemoryStore(RateLimiterStoreABC):
43
+ """An in memory store of rate limits."""
44
+
45
+ def __init__(self) -> None:
46
+ self._tats: Dict[str, datetime] = {}
47
+
48
+ async def get(self, key: str, default: datetime) -> datetime:
49
+ return self._tats.get(key, default)
50
+
51
+ async def set(self, key: str, tat: datetime) -> None:
52
+ self._tats[key] = tat
53
+
54
+ async def before_serving(self) -> None:
55
+ pass
56
+
57
+ async def after_serving(self) -> None:
58
+ pass
@@ -0,0 +1,33 @@
1
+ [check-manifest]
2
+ ignore = tox.ini
3
+
4
+ [flake8]
5
+ ignore = E203, E252, W503, W504
6
+ max_line_length = 100
7
+
8
+ [mypy]
9
+ allow_redefinition = True
10
+ disallow_subclassing_any = True
11
+ disallow_untyped_defs = True
12
+ strict_equality = True
13
+ strict_optional = False
14
+ warn_redundant_casts = True
15
+ warn_unused_configs = True
16
+ warn_unused_ignores = True
17
+
18
+ [mypy-_pytest.*]
19
+ ignore_missing_imports = True
20
+
21
+ [mypy-aioredis.*]
22
+ ignore_missing_imports = True
23
+
24
+ [mypy-pytest.*]
25
+ ignore_missing_imports = True
26
+
27
+ [tool:pytest]
28
+ addopts = --showlocals
29
+
30
+ [egg_info]
31
+ tag_build =
32
+ tag_date = 0
33
+
@@ -0,0 +1,51 @@
1
+ import os
2
+ import sys
3
+
4
+ from setuptools import setup, find_packages
5
+
6
+ if sys.version_info < (3,7,0):
7
+ sys.exit("Python 3.7.0 is the minimum required version")
8
+
9
+ PROJECT_ROOT = os.path.dirname(__file__)
10
+
11
+ with open(os.path.join(PROJECT_ROOT, "README.rst")) as file_:
12
+ long_description = file_.read()
13
+
14
+ INSTALL_REQUIRES = [
15
+ "Quart>=0.7",
16
+ ]
17
+
18
+ setup(
19
+ name="Quart-Rate-Limiter",
20
+ version="0.1.0",
21
+ python_requires=">=3.7.0",
22
+ description="A Quart extension to provide rate limiting support.",
23
+ long_description=long_description,
24
+ url="https://gitlab.com/pgjones/quart-rate-limiter/",
25
+ author="P G Jones",
26
+ author_email="philip.graham.jones@googlemail.com",
27
+ license="MIT",
28
+ classifiers=[
29
+ "Development Status :: 3 - Alpha",
30
+ "Environment :: Web Environment",
31
+ "Intended Audience :: Developers",
32
+ "License :: OSI Approved :: MIT License",
33
+ "Operating System :: OS Independent",
34
+ "Programming Language :: Python",
35
+ "Programming Language :: Python :: 3",
36
+ "Programming Language :: Python :: 3.7",
37
+ "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
38
+ "Topic :: Software Development :: Libraries :: Python Modules",
39
+ ],
40
+ packages=find_packages(exclude=["tests", "tests.*"]),
41
+ py_modules=["quart_rate_limiter"],
42
+ install_requires=INSTALL_REQUIRES,
43
+ extras_require={
44
+ "redis": ["aioredis"],
45
+ },
46
+ tests_require=INSTALL_REQUIRES + [
47
+ "pytest",
48
+ "pytest-asyncio",
49
+ ],
50
+ include_package_data=True,
51
+ )
@@ -0,0 +1,56 @@
1
+ from datetime import datetime, timedelta
2
+
3
+ import pytest
4
+ from _pytest.monkeypatch import MonkeyPatch
5
+ from quart import Quart, ResponseReturnValue
6
+
7
+ import quart_rate_limiter
8
+ from quart_rate_limiter import rate_limit, RateLimiter
9
+
10
+
11
+ @pytest.fixture(name="fixed_datetime")
12
+ def _fixed_datetime(monkeypatch: MonkeyPatch) -> datetime:
13
+ class MockDatetime(datetime):
14
+ @classmethod
15
+ def utcnow(cls) -> datetime:
16
+ return datetime(2019, 3, 4)
17
+
18
+ monkeypatch.setattr(quart_rate_limiter, "datetime", MockDatetime)
19
+ return MockDatetime.utcnow()
20
+
21
+
22
+ @pytest.fixture(name="app")
23
+ def _app() -> Quart:
24
+ app = Quart(__name__)
25
+
26
+ @app.route("/rate_limit/")
27
+ @rate_limit(1, timedelta(seconds=2))
28
+ @rate_limit(10, timedelta(seconds=20))
29
+ async def index() -> ResponseReturnValue:
30
+ return ""
31
+
32
+ RateLimiter(app)
33
+ return app
34
+
35
+
36
+ @pytest.mark.asyncio
37
+ async def test_rate_limit(app: Quart, fixed_datetime: datetime) -> None:
38
+ test_client = app.test_client()
39
+ response = await test_client.get("/rate_limit/")
40
+ assert response.status_code == 200
41
+ assert response.headers["RateLimit-Limit"] == "1"
42
+ assert response.headers["RateLimit-Remaining"] == "0"
43
+ assert response.headers["RateLimit-Reset"] == "2"
44
+
45
+ response = await test_client.get("/rate_limit/")
46
+ assert response.status_code == 429
47
+ assert response.headers["Retry-After"] == "2"
48
+
49
+
50
+ @pytest.mark.asyncio
51
+ async def test_rate_limit_unique_keys(app: Quart, fixed_datetime: datetime) -> None:
52
+ test_client = app.test_client()
53
+ response = await test_client.get("/rate_limit/", headers={"Remote-Addr": "127.0.0.1"})
54
+ assert response.status_code == 200
55
+ response = await test_client.get("/rate_limit/", headers={"Remote-Addr": "127.0.0.2"})
56
+ assert response.status_code == 200