This library simplifies an interaction between microservices:
- Allows sending requests using various strategies
- Propagates a deadline and a priority of requests
- 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
The following strategies are supported:
- Single attempt. Only one attempt is sent.
- Sequential. Attempts are sent sequentially with delays between them.
- 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
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()])
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()
]
)
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%).