Skip to main content

A Quart extension to provide rate limiting support.

Project description

Build Status pypi python license

Quart-Rate-Limiter is an extension for Quart to allow for rate limits to be defined and enforced on a per route basis. The 429 error response includes a RFC7231 compliant Retry-After header and the successful responses contain headers compliant with the RateLimit Header Fields for HTTP RFC draft.

Usage

To add a rate limit first initialise the RateLimiting extension with the application,

app = Quart(__name__)
rate_limiter = RateLimiter(app)

or via the factory pattern,

rate_limiter = RateLimiter()

def create_app():
    app = Quart(__name__)
    rate_limiter.init_app(app)
    return app

Now this is done you can apply rate limits to any route by using the rate_limit decorator,

@app.route('/')
@rate_limit(1, timedelta(seconds=10))
async def handler():
    ...

Or to apply rate limits to all routes within a blueprint by using the limit_blueprint function,

blueprint = Blueprint("name", __name__)
limit_blueprint(blueprint, 1, timedelta(seconds=10))

Or to apply rate limits to all routes in an app, define the default limits when initialising the RateLimiter,

rate_limiter = RateLimiter(
    default_limits=[RateLimit(1, timedelta(seconds=10))]
)

and then to exempt a route,

@app.route("/exempt")
@rate_exempt
async def handler():
    ...

To alter the identification of remote users you can either supply a global key function when initialising the extension, or on a per route basis.

By default rate limiting information (TATs) will be stored in memory, which will result in unexpected behaviour if multiple workers are used. To solve this a redis store can be used by installing the redis extra (pip install quart-rate-limiter[redis]) and then using as so,

from quart_rate_limiter.redis_store import RedisStore

redis_store = RedisStore(address)
RateLimiter(app, store=redis_store)

This store uses aioredis, and any extra keyword arguments passed to the RedisStore constructor will be passed to the aioredis create_redis function.

A custom store is possible, see the RateLimiterStoreABC for the required interface.

Simple examples

To limit a route to 1 request per second and a maximum of 20 per minute,

@app.route('/')
@rate_limit(1, timedelta(seconds=1))
@rate_limit(20, timedelta(minutes=1))
async def handler():
    ...

Alternatively the limits argument can be used for multiple limits,

@app.route('/')
@rate_limit(
    limits=[
        RateLimit(1, timedelta(seconds=1)),
        RateLimit(20, timedelta(minutes=1)),
    ],
)
async def handler():
    ...

To identify remote users based on the forwarded IP, rather than the direct IP (if behind a load balancer),

async def key_function():
    # Return the X-Forwarded-For as the user-agent identifier,
    # unless it isn't present (direct connection).
    return request.headers.get("X-Forwarded-For", request.remote_addr)

RateLimiter(app, key_function=key_function)

The key_function is a coroutine function to allow session lookups if appropriate.

Contributing

Quart-Rate-Limiter is developed on GitLab. You are very welcome to open issues or propose merge requests.

Testing

The best way to test Quart-Rate-Limiter is with Tox,

$ pip install tox
$ tox

this will check the code style and run the tests.

Help

This README is the best place to start, after that try opening an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Quart-Rate-Limiter-0.4.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Quart_Rate_Limiter-0.4.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file Quart-Rate-Limiter-0.4.1.tar.gz.

File metadata

  • Download URL: Quart-Rate-Limiter-0.4.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.2

File hashes

Hashes for Quart-Rate-Limiter-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b1d0b1fa0a7b3b6731fa3613868df023bc8de38a45defbf1568d9d0ba10c4f46
MD5 cc2dc8191b5358042cd91037c2eac1a5
BLAKE2b-256 319c3dd23b98f99424c71e18fad8b6959d37d7e72f2c1a12370a8e76fff552a2

See more details on using hashes here.

File details

Details for the file Quart_Rate_Limiter-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: Quart_Rate_Limiter-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.2

File hashes

Hashes for Quart_Rate_Limiter-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b0cca6d2c319e0866f11cf00610b5864fb335449cab39d7d2060d2f4ece5ae2
MD5 5b0e0536961b3a690ee7fbadc243bcd5
BLAKE2b-256 ddbfc73f97aa80a668ef7a093ebcbe20fa2734672a8a0757a07a781ded63fbfc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page