Skip to main content

Various strategies for sending requests

Project description

aio-request

This library simplifies an interaction between microservices:

  1. Allows sending requests using various strategies
  2. Propagates a deadline and a priority of requests
  3. Exposes client/server metrics

Example:

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

Request strategies

The following strategies are supported:

  1. Single attempt. Only one attempt is sent.
  2. Sequential. Attempts are sent sequentially with delays between them.
  3. Parallel. Attempts are sent in parallel one by one with delays between them.

Attempts count and delays are configurable.

Example:

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_backoff_delays(min_delay_seconds=0.1, delay_multiplier=0.1)
        )
    )
    async with response_ctx as response:
        pass  # process response here

Deadline & priority propagation

To enable it for the server side a middleware should be configured:

import aiohttp.web
import aio_request

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

Expose client/server metrics

To enable client metrics, just install prometheus-client

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/",
    )

It is an example of how it should be done for aiohttp and prometheus.

To enable client metrics just install prometheus-client and use the following code:

import aiohttp.web
import aio_request

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

Circuit breaker

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/",
        circuit_breaker=aio_request.DefaultCircuitBreaker[str, int](
            break_duration=1.0,
            sampling_duration=1.0,
            minimum_throughput=2,
            failure_threshold=0.5,
        ),
    )

In the case of requests count >= minimum throughput(>=2) in sampling period(1 second) the circuit breaker will open if failed requests count/total requests count >= failure threshold(50%).

Project details


Release history Release notifications | RSS feed

This version

0.2.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aio_request-0.2.9.tar.gz (134.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aio_request-0.2.9-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file aio_request-0.2.9.tar.gz.

File metadata

  • Download URL: aio_request-0.2.9.tar.gz
  • Upload date:
  • Size: 134.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for aio_request-0.2.9.tar.gz
Algorithm Hash digest
SHA256 e9186f5b5a110d2406bbce69e33db8e301349007b895ec1a0b0417b87f2db833
MD5 51c9cf76b26cec125bdb2f26a75cea1c
BLAKE2b-256 bc41c37718c66ed0f10812ab3b825318a249e9d41628d4408555d70537007fed

See more details on using hashes here.

File details

Details for the file aio_request-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: aio_request-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for aio_request-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d25041c3a17f1bc15eb1e7207678ffd78e8322341adba7c5d573c8a286916137
MD5 28a20381e99c08a80c8426d8aa8ea921
BLAKE2b-256 6e46b648e64b673b8d97cc276e9f1489d4400ca8f101b75bfa93b2826cf41829

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page