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_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 a metrics provider should be passed to the transport:

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

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

To enable client metrics a metrics provider should be passed to the middleware:

import aiohttp.web
import aio_request

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

Circuit breaker

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

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%).

v0.1.23 (2022-02-08)

v0.1.22 (2022-01-08)

v0.1.21 (2022-01-05)

  • Content type should be None in Response.json()

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)

  • Fix Request.update_headers, add Request.extend_headers #59

v0.1.10 (2021-07-20)

  • Add Response.is_json property to check whether content-type is json compatible #58
  • Tracing support #54,
  • Configuration of a new pipeline

Project details


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.1.23.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

aio_request-0.1.23-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file aio-request-0.1.23.tar.gz.

File metadata

  • Download URL: aio-request-0.1.23.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for aio-request-0.1.23.tar.gz
Algorithm Hash digest
SHA256 4d581103eecd39b9f65c8e69b5781f745d5312b23d00ff6cda935da83122eb0a
MD5 3da133c4b33857933c3d2a74adda0b9d
BLAKE2b-256 483cd51834fc6107080e32c4cce43d6811ebba86499a03d77cd5f00c11f0e3f8

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: aio_request-0.1.23-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for aio_request-0.1.23-py3-none-any.whl
Algorithm Hash digest
SHA256 740547a623dd3f606fca1e3060ffe2d84677cec11db1dfac4ea953752832824b
MD5 5f8a84a74aa8eb68e53788b3f582a3ba
BLAKE2b-256 0456210bb86ac9f6f5fa3fc4db8193a16452e343d47de3567ceebc864a24a67c

See more details on using hashes here.

Provenance

Supported by

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