跳转到主要内容

发送请求的各种策略

项目描述

aio-request

此库简化了微服务之间的交互

  1. 允许使用各种策略发送请求
  2. 传播请求的截止日期和优先级
  3. 公开客户端/服务器指标

示例

import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
    )
    response_ctx = client.request(
        aio_request.get("thing"),
        deadline=aio_request.Deadline.from_timeout(5)
    )
    async with response_ctx as response:
        pass  # process response here

请求策略

以下策略得到支持

  1. 单次尝试。只发送一次尝试。
  2. 顺序。尝试之间有延迟,顺序发送。
  3. 并行。尝试之间有延迟,逐个并行发送。

尝试次数和延迟是可配置的。

示例

import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
    )
    response_ctx = client.request(
        aio_request.get("thing"),
        deadline=aio_request.Deadline.from_timeout(5),
        strategy=aio_request.parallel_strategy(
            attempts_count=3,
            delays_provider=aio_request.linear_delays(min_delay_seconds=0.1, delay_multiplier=0.1)
        )
    )
    async with response_ctx as response:
        pass  # process response here

截止日期 & 优先级传播

要为服务器端启用它,需要配置中间件

import aiohttp.web
import aio_request

app = aiohttp.web.Application(middlewares=[aio_request.aiohttp_middleware_factory()])

公开客户端/服务器指标

要启用客户端指标,需要将指标提供程序传递给传输

import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(
            client_session,
            metrics_provider=aio_request.PROMETHEUS_METRICS_PROVIDER
        ),
        endpoint="http://endpoint:8080/",
    )

这是为aiohttp和prometheus执行此操作的示例。

要启用客户端指标,需要将指标提供程序传递给中间件

import aiohttp.web
import aio_request

app = aiohttp.web.Application(
    middlewares=[
        aio_request.aiohttp_middleware_factory(
            metrics_provider=aio_request.PROMETHEUS_METRICS_PROVIDER
        )
    ]
)

断路器

import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup_v2(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
        circuit_breaker=aio_request.DefaultCircuitBreaker[str, int](
            break_duration=1.0,
            sampling_duration=1.0,
            minimum_throughput=2,
            failure_threshold=0.5,
        ),
    )

在请求次数 >= 最小吞吐量(>=2)的情况下,如果失败请求次数/总请求次数 >= 失败阈值(50%),在采样周期(1秒)内,如果失败请求次数/总请求次数 >= 失败阈值(50%),则断路器将打开。

v0.1.31 (2024-09-05)

v0.1.30 (2023-07-23)

v0.1.29 (2023-04-27)

v0.1.28 (2023-04-27)

v0.1.27 (2023-02-16)

v0.1.26 (2022-11-02)

v0.1.25 (2022-08-25)

v0.1.24 (2022-07-04)

v0.1.23 (2022-02-08)

v0.1.22 (2022-01-08)

v0.1.21 (2022-01-05)

  • Response.json()中的内容类型应为None

v0.1.20 (2022-01-05)

v0.1.19 (2021-11-01)

v0.1.18 (2021-09-08)

v0.1.17 (2021-09-01)

v0.1.16 (2021-09-01)

v0.1.15 (2021-09-01)

v0.1.14 (2021-08-18)

v0.1.13 (2021-08-15)

v0.1.12 (2021-07-21)

v0.1.11 (2021-07-21)

  • 修复Request.update_headers,添加Request.extend_headers #59

v0.1.10 (2021-07-20)

  • 添加Response.is_json属性,以检查内容类型是否与json兼容 #58
  • 跟踪支持 #54
  • 配置 新的流水线

项目详情


发布历史 发布通知 | RSS源

下载文件

下载适合您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装软件包 的信息。

源分布

aio-request-0.1.31.tar.gz (21.2 kB 查看哈希值)

上传时间

构建分布

aio_request-0.1.31-py3-none-any.whl (25.2 kB 查看哈希值)

上传时间 Python 3

由...