quart-rate-limiter 0.1.0__py3-none-any.whl

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,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,164 @@
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
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Environment :: Web Environment
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.7
18
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
19
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Python: >=3.7.0
21
+ Requires-Dist: Quart (>=0.7)
22
+ Provides-Extra: redis
23
+ Requires-Dist: aioredis ; extra == 'redis'
24
+
25
+ Quart-Rate-Limiter
26
+ ==================
27
+
28
+ |Build Status| |pypi| |python| |license|
29
+
30
+ Quart-Rate-Limiter is an extension for `Quart
31
+ <https://gitlab.com/pgjones/quart>`_ to allow for rate limits to be
32
+ defined and enforced on a per route basis. The 429 error response
33
+ includes a `RFC7231
34
+ <https://tools.ietf.org/html/rfc7231#section-7.1.3>`_ compliant
35
+ ``Retry-After`` header and the successful responses contain headers
36
+ compliant with the `RateLimit Header Fields for HTTP
37
+ <https://tools.ietf.org/html/draft-polli-ratelimit-headers-00>`_ RFC
38
+ draft.
39
+
40
+ Usage
41
+ -----
42
+
43
+ To add a rate limit first initialise the RateLimiting extension with
44
+ the application,
45
+
46
+ .. code-block:: python
47
+
48
+ app = Quart(__name__)
49
+ rate_limiter = RateLimiter(app)
50
+
51
+ or via the factory pattern,
52
+
53
+ .. code-block:: python
54
+
55
+ rate_limiter = RateLimiter()
56
+
57
+ def create_app():
58
+ app = Quart(__name__)
59
+ rate_limiter.init_app(app)
60
+ return app
61
+
62
+ Now this is done you can apply rate limits to any route by using the
63
+ ``rate_limit`` decorator,
64
+
65
+ .. code-block:: python
66
+
67
+ @app.route('/')
68
+ @rate_limit(1, timedelta(seconds=10))
69
+ async def handler():
70
+ ...
71
+
72
+ To alter the identification of remote users you can either supply a
73
+ global key function when initialising the extension, or on a per route
74
+ basis.
75
+
76
+ By default rate limiting information (TATs) will be stored in memory,
77
+ which will result in unexpected behaviour if multiple workers are
78
+ used. To solve this a redis store can be used by installing the
79
+ ``redis`` extra (``pip install quart-rate-limiter[redis]``) and then
80
+ using as so,
81
+
82
+ .. code-block:: python
83
+
84
+ from quart_rate_limiter.redis_store import RedisStore
85
+
86
+ redis_store = RedisStore(address)
87
+ RateLimiter(app, store=redis_store)
88
+
89
+ This store uses `aioredis <https://github.com/aio-libs/aioredis>`_,
90
+ and any extra keyword arguments passed to the ``RedisStore``
91
+ constructor will be passed to the aioredis ``create_redis`` function.
92
+
93
+ A custom store is possible, see the ``RateLimiterStoreABC`` for the
94
+ required interface.
95
+
96
+ Simple examples
97
+ ~~~~~~~~~~~~~~~
98
+
99
+ To limit a route to 1 request per second and a maximum of 20 per minute,
100
+
101
+ .. code-block:: python
102
+
103
+ @app.route('/')
104
+ @rate_limit(1, timedelta(seconds=1))
105
+ @rate_limit(20, timedelta(minutes=1))
106
+ async def handler():
107
+ ...
108
+
109
+ To identify remote users based on the forwarded IP, rather than the
110
+ direct IP (if behind a load balancer),
111
+
112
+ .. code-block:: python
113
+
114
+ async def key_function():
115
+ # Return the X-Forwarded-For as the user-agent identifier,
116
+ # unless it isn't present (direct connection).
117
+ return request.headers.get("X-Forwarded-For", request.remote_addr)
118
+
119
+ RateLimiter(app, key_function=key_function)
120
+
121
+ The ``key_function`` is a coroutine function to allow session lookups
122
+ if appropriate.
123
+
124
+ Contributing
125
+ ------------
126
+
127
+ Quart-Rate-Limiter is developed on `GitLab
128
+ <https://gitlab.com/pgjones/quart-rate-limiter>`_. You are very welcome to
129
+ open `issues <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_ or
130
+ propose `merge requests
131
+ <https://gitlab.com/pgjones/quart-rate-limiter/merge_requests>`_.
132
+
133
+ Testing
134
+ ~~~~~~~
135
+
136
+ The best way to test Quart-Rate-Limiter is with Tox,
137
+
138
+ .. code-block:: console
139
+
140
+ $ pip install tox
141
+ $ tox
142
+
143
+ this will check the code style and run the tests.
144
+
145
+ Help
146
+ ----
147
+
148
+ This README is the best place to start, after that try opening an
149
+ `issue <https://gitlab.com/pgjones/quart-rate-limiter/issues>`_.
150
+
151
+
152
+ .. |Build Status| image:: https://gitlab.com/pgjones/quart-rate-limiter/badges/master/build.svg
153
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/commits/master
154
+
155
+ .. |pypi| image:: https://img.shields.io/pypi/v/quart-rate-limiter.svg
156
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
157
+
158
+ .. |python| image:: https://img.shields.io/pypi/pyversions/quart-rate-limiter.svg
159
+ :target: https://pypi.python.org/pypi/Quart-Rate-Limiter/
160
+
161
+ .. |license| image:: https://img.shields.io/badge/license-MIT-blue.svg
162
+ :target: https://gitlab.com/pgjones/quart-rate-limiter/blob/master/LICENSE
163
+
164
+
@@ -0,0 +1,9 @@
1
+ quart_rate_limiter/__init__.py,sha256=wkXuoFOjMYAUKaV6X_6xU-MyNcuxJ7Xj_A_Yb3W6POU,7033
2
+ quart_rate_limiter/py.typed,sha256=sow9soTwP9T_gEAQSVh7Gb8855h04Nwmhs2We-JRgZM,7
3
+ quart_rate_limiter/redis_store.py,sha256=42S416LC7FDRTfU5hDecgxO5CzkI0fl6N1ekvDuntZ8,1169
4
+ quart_rate_limiter/store.py,sha256=BAi1GxOZ4pEMZdZzCiUMn3Y7JRgSakLLowN8pBqvUVg,1573
5
+ Quart_Rate_Limiter-0.1.0.dist-info/LICENSE,sha256=6h3uzV8K7bKXEpEo2itfCYU8CjI7VIUuhS19Knf6eRg,1050
6
+ Quart_Rate_Limiter-0.1.0.dist-info/METADATA,sha256=rxGJSAqS-2Wk21jlxiPyR7cdHbWq6JJW0feRNoYc_EI,4927
7
+ Quart_Rate_Limiter-0.1.0.dist-info/WHEEL,sha256=S8S5VL-stOTSZDYxHyf0KP7eds0J72qrK0Evu3TfyAY,92
8
+ Quart_Rate_Limiter-0.1.0.dist-info/top_level.txt,sha256=gxwF5rNISjD6RqC737rtIJoe1c1kQywRr10979KFafs,19
9
+ Quart_Rate_Limiter-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.33.4)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ quart_rate_limiter
@@ -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