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)
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
from aiohttp import web
from aiohttp.web_app import Application
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aio_throttle.aiohttp import throttling_middleware, setup_throttling
from aio_throttle.prometheus import prometheus_aware_throttler, build_throttle_results_counter
throttle_results_counter = build_throttle_results_counter()
async def ok(_: Request) -> Response:
return Response()
async def create_app() -> Application:
app = web.Application(middlewares=[throttling_middleware()])
setup_throttling(app, extensions=[prometheus_aware_throttler(throttle_results_counter=throttle_results_counter)])
app.router.add_get("/", ok)
return app
web.run_app(create_app(), port=8080, access_log=None)
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.4.1.tar.gz
(6.5 kB
view details)
Built Distribution
File details
Details for the file aio-throttle-1.4.1.tar.gz
.
File metadata
- Download URL: aio-throttle-1.4.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87d9881580838777f24cb4a106420bf67563ec009b5b4db670675b4302dd2145 |
|
MD5 | cec51bb0e291db2a6034ff2755b5d07d |
|
BLAKE2b-256 | a983da4ac8a4c8e568704410778f0a446bdbf38a06107d97f9c5e48fcf2255eb |
Provenance
File details
Details for the file aio_throttle-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: aio_throttle-1.4.1-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f3b85f60c4b65b9d442c781161e6230f2a785583215c566e5f9f76a01b56006 |
|
MD5 | e57f088ca2b6757dbb65f9147568959d |
|
BLAKE2b-256 | 3ec8a0700626706d3a8dfa471c85c568e7c4854d73112aba632a5d4cac1f910a |