A simple throttling package
Project description
This library is inspired by this book and this implementation https://github.com/vostok/throttling.
Features:
- Set capacity(max parallel requests) and queue(max queued requests) limits.
- Per-consumer limits. For instance, to not allow any consumer to use more than 70% of service's capacity.
- 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
Release history Release notifications | RSS feed
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.2.1.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for aio_throttle-1.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28ffac30d3b25296f0e5ab6a018c49f1bcc6855562d6e5a899cb1231bf287240 |
|
MD5 | 93e0fb01c6a555a86aa9147c0f6a18b4 |
|
BLAKE2b-256 | f2a0449b74591bb3369ad0ec13b448e9a62ad1f48dc4a7ab47147baeea4b4c9b |