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. Sequential. Attempts are sent sequentially with delays between them.
  2. 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
        )
    ]
)

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

Uploaded Source

Built Distribution

aio_request-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aio-request-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for aio-request-0.1.0.tar.gz
Algorithm Hash digest
SHA256 81d52c47c6ac5feedca812226a453a5e3fa9050b08fefea76daf835aa5c57309
MD5 1206c93b666f14edff88a93340cc9a37
BLAKE2b-256 73b22fd7554d79a692e1a147ec2cf479c3f805de985e2ca0de12ec5e17f302f1

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: aio_request-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for aio_request-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6858c068687ebb0dc54c010be40c9a9d044c4b30a333a86d7fa46ae3ae3c047c
MD5 9b10d3f8afa0e8c603454e68f7a43a7d
BLAKE2b-256 62e57e75aec0b271566db1a7a91cd8e82ce300d2b45ed9d36097587992ffad4c

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