简单的节流包
项目描述
此库受以下书籍和此实现 https://github.com/vostok/throttling 的启发。
特性
- 设置容量(最大并行请求)和队列(最大队列请求)限制。
- 每个消费者限制。例如,不允许任何消费者使用超过服务容量的70%。
- 每个请求的优先级。例如,不允许优先级最低的请求入队,也不允许优先级正常的请求使用超过90%的服务容量。
示例
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
与aiohttp和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)
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码发行版
aio-throttle-1.7.0.tar.gz (8.3 kB 查看哈希值)
编译发行版
aio_throttle-1.7.0-py3-none-any.whl (10.1 kB 查看哈希值)