Skip to main content

A simple throttling package

Project description

This library is inspired by this book and this implementation https://github.com/vostok/throttling.

Features:

  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.

Example:

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=[
        aio_throttle.aiohttp_middleware_factory(
                capacity_limit=20,
                queue_limit=100,
                consumer_quotas=[aio_throttle.MaxFractionCapacityQuota[str](0.7)],
                priority_quotas=[
                    aio_throttle.MaxFractionCapacityQuota[aio_throttle.ThrottlePriority](
                        0.9, aio_throttle.ThrottlePriority.NORMAL
                    )
                ],
                metrics_provider=aio_throttle.PROMETHEUS_METRICS_PROVIDER,
            ),
    ])
    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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file aio-throttle-1.7.0.tar.gz.

File metadata

  • Download URL: aio-throttle-1.7.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for aio-throttle-1.7.0.tar.gz
Algorithm Hash digest
SHA256 ce969ebb149ffe59231008a71917165893a38c40b49c3e5201c24209a49b9829
MD5 c7afc4c2c46c36f48989294ec22e5127
BLAKE2b-256 bc0f72748a063f9caa57fbd18cb8eeebc2c41b570f88dfc68c0cb47acd869611

See more details on using hashes here.

File details

Details for the file aio_throttle-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: aio_throttle-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for aio_throttle-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b96d8c3f999be21e060ba013da6601628973cacb61633ce17fc07f80f821716
MD5 dd4e245e6c43a23acfd96168fa9dda9f
BLAKE2b-256 1bf3e4145220b66fa9dd773434cbbda69742454a601531b818a4513ec64b1651

See more details on using hashes here.

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