Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
0.13.0 TBD
-----------

* Add WebSocket rate limiting support with ``WebSocketRateLimiter`` class.
* Add ``WebSocketRateLimitException`` for WebSocket-specific rate limit errors.
* Support same storage backends (Memory, Redis, Valkey) for WebSocket rate limiting.
* Add ``websocket_key_function`` for default WebSocket connection identification.
* Compatible API with fastapi-limiter's WebSocketRateLimiter.

0.12.1 2025-08-13
-----------------

Expand Down
38 changes: 36 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Quart-Rate-Limiter

Quart-Rate-Limiter is an extension for `Quart
<https://github.com/pgjones/quart>`_ to allow for rate limits to be
defined and enforced on a per route basis. The 429 error response
includes a `RFC7231
defined and enforced on a per route basis and for WebSocket connections.
The 429 error response includes a `RFC7231
<https://tools.ietf.org/html/rfc7231#section-7.1.3>`_ compliant
``Retry-After`` header and the successful responses contain headers
compliant with the `RateLimit Header Fields for HTTP
Expand Down Expand Up @@ -69,6 +69,40 @@ their IP,
The ``key_function`` is a coroutine function to allow session lookups
if appropriate.

WebSocket Rate Limiting
~~~~~~~~~~~~~~~~~~~~~~~

Quart-Rate-Limiter also supports rate limiting for WebSocket connections,
similar to fastapi-limiter's WebSocketRateLimiter:

.. code-block:: python

from quart_rate_limiter import RateLimiter, WebSocketRateLimiter, WebSocketRateLimitException
from quart_rate_limiter.redis_store import RedisStore

# Configure global rate limiter with Redis
redis_store = RedisStore("redis://localhost:6379/0")
RateLimiter(app, store=redis_store)

@app.websocket('/ws')
async def websocket_endpoint():
# Automatically uses the same Redis store as the global RateLimiter
ratelimit = WebSocketRateLimiter(times=1, seconds=5)

await websocket.accept()
while True:
try:
data = await websocket.receive()
await ratelimit(websocket, context_key=data) # context_key is optional
await websocket.send(f"Hello, world! You sent: {data}")
except WebSocketRateLimitException:
await websocket.send("Rate limited! Please slow down.")

WebSocket rate limiting automatically uses the same storage backend as your
global ``RateLimiter`` configuration, ensuring consistency across HTTP and
WebSocket rate limiting. It supports all the same storage backends (Memory, Redis, Valkey)
and key functions as HTTP route rate limiting.

Contributing
------------

Expand Down
11 changes: 11 additions & 0 deletions docs/how_to_guides/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ configuration
Configuration key type default
--------------------------- ------------ ----------------
QUART_RATE_LIMITER_ENABLED bool True
QUART_RATE_LIMITER_STORE Store None
=========================== ============ ================

``QUART_RATE_LIMITER_ENABLED`` is most useful for testing as it
prevents rate limits, which may depend on test timing, from mistakenly
failing tests.

``QUART_RATE_LIMITER_STORE`` is automatically set when you initialize
a ``RateLimiter`` instance with a custom store. This configuration value
is used by ``WebSocketRateLimiter`` to automatically detect and use the
same store for WebSocket rate limiting, ensuring consistency across HTTP
and WebSocket rate limiting in your application.

.. note::
You should not set ``QUART_RATE_LIMITER_STORE`` manually. It is
automatically managed by the ``RateLimiter`` during initialization.
1 change: 1 addition & 0 deletions docs/how_to_guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ How to guides
key.rst
skip.rst
store.rst
websockets.rst
Loading