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)

async with throttler.throttle(consumer, priority) as result:
    ... # check if result is ThrottleResult.ACCEPTED or not

Example of integration with aiohttp:

from typing import Awaitable, Callable

from aiohttp import web
from aiohttp.web_middlewares import middleware
from aiohttp.web_request import Request
from aiohttp.web_response import StreamResponse

from aio_throttle import Throttler, ThrottlePriority

Handler = Callable[[Request], Awaitable[StreamResponse]]


@middleware
async def middleware(request: Request, handler: Handler) -> StreamResponse:
    throttler: Throttler = request.app['throttler']
    service = request.headers.get('X-Service', 'UNKNOWN')
    priority = ThrottlePriority(request.headers.get('X-Request-Priority', ThrottlePriority.NORMAL))
    async with throttler.throttle(service, priority) as result:
        if not result:
            return web.Response(status=503)
        return await handler(request)

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.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

aio_throttle-1.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aio-throttle-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for aio-throttle-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8a1b4bd60ee86efe340797551751df0c3618cf9f7513d0e4f627a906318a2126
MD5 c8de0dd07e9319d224768e65e85282ea
BLAKE2b-256 8418eb8d53e3e504da5850ee004f1ca2b7d966f415e59e1210f9b3c6b1b244d6

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: aio_throttle-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for aio_throttle-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0b0b577fd62ad80361076648cff94823b321787fdb5d74b6448ae455c48feec
MD5 fa2f0a55897db3bae7c5389f7d777cff
BLAKE2b-256 458f9bbf3da60a63bd16bcb84c305f5546de64e4f6e5531bef99090d4f969190

See more details on using hashes here.

Provenance

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