Skip to main content

A simple throttling package

Project description

This library is inspired by this book and this implementation


  1. Set capacity(max parallel requests) and queue(max queued requests) limits.
  2. Per-consumer limits. For instance, to not allow any consumer to use more than 70% of service's capacity.
  3. Per-request priorities. For instance, to not allow requests with lowest priority to be queued or to now allow requests with normal priority to use more than 90% of service's capacity.


from aio_throttle import Throttler, MaxFractionCapacityQuota, ThrottlePriority, ThrottleResult

capacity_limit = 100
queue_limit = 200
consumer_quotas = [MaxFractionCapacityQuota(0.7)]
priority_quotas = [MaxFractionCapacityQuota(0.9, ThrottlePriority.NORMAL)]
throttler = Throttler(capacity_limit, queue_limit, consumer_quotas, priority_quotas)

consumer, priority = "yet another consumer", ThrottlePriority.HIGH
async with throttler.throttle(consumer=consumer, priority=priority) as result:
    ... # check if result is ThrottleResult.ACCEPTED or not

Example of an integration with aiohttp and prometheus_client()

import aiohttp
import aiohttp.web
import aiohttp.web_request
import aiohttp.web_response

import aio_throttle

@aio_throttle.aiohttp_ignore() # do not throttle this handler 
async def healthcheck(_: aiohttp.web_request.Request) -> aiohttp.web_response.Response:
    return aiohttp.web_response.Response()

async def authorize(_: aiohttp.web_request.Request) -> aiohttp.web_response.Response:
    return aiohttp.web_response.Response()

async def create_app() -> aiohttp.web.Application:
    app = aiohttp.web.Application(middlewares=[
                        0.9, aio_throttle.ThrottlePriority.NORMAL
    app.router.add_get("/healthcheck", healthcheck)
    app.router.add_post("/authorize", authorize)
    return app

aiohttp.web.run_app(create_app(), port=8080, access_log=None)

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

aio-throttle-1.7.0.tar.gz (8.3 kB view hashes)

Uploaded Source

Built Distribution

aio_throttle-1.7.0-py3-none-any.whl (10.1 kB view hashes)

Uploaded Python 3

Supported by

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