-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from dapper91/dev
- JSON-RPC client requests retry support added - aio-pika integration and backend updated for aio-pika 8.0 - type aliases for middlewares added - httpx minimal version updated due to found vulnerability
- Loading branch information
Showing
17 changed files
with
821 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
.. _retires: | ||
|
||
Retries | ||
======= | ||
|
||
``pjrpc`` supports request retries based on response code or received exception using customizable backoff strategy. | ||
``pjrpc`` provides several built-in backoff algorithms (see :py:mod:`pjrpc.client.retry`), but you can | ||
implement your own one like this: | ||
|
||
.. code-block:: python | ||
import dataclasses as dc | ||
import random | ||
from pjrpc.client.retry import Backoff | ||
@dc.dataclass(frozen=True) | ||
class RandomBackoff(Backoff): | ||
def __call__(self) -> Iterator[float]: | ||
return (random.random() for _ in range(self.attempts)) | ||
Retry strategy can be configured for all client requests by passing a strategy to a client constructor | ||
as a `retry_strategy` argument or for a particular request as a `_retry_strategy` when calling `send` method. | ||
|
||
The following example illustrate request retries api usage: | ||
|
||
.. code-block:: python | ||
import asyncio | ||
import random | ||
import pjrpc | ||
from pjrpc.client.backend import aiohttp as pjrpc_client | ||
from pjrpc.client.retry import ExponentialBackoff, PeriodicBackoff, RetryStrategy | ||
async def main(): | ||
default_retry_strategy = RetryStrategy( | ||
exceptions={TimeoutError}, | ||
backoff=PeriodicBackoff(attempts=3, interval=1.0, jitter=lambda: random.gauss(mu=0.5, sigma=0.1)), | ||
) | ||
async with pjrpc_client.Client('http://localhost/api/v1', retry_strategy=default_retry_strategy) as client: | ||
response = await client.send( | ||
pjrpc.Request('sum', params=[1, 2], id=1), | ||
_retry_strategy=RetryStrategy( | ||
exceptions={TimeoutError}, | ||
codes={2001}, | ||
backoff=ExponentialBackoff( | ||
attempts=3, base=1.0, factor=2.0, jitter=lambda: random.gauss(mu=0.5, sigma=0.1), | ||
), | ||
), | ||
) | ||
print(f"1 + 2 = {response.result}") | ||
result = await client.proxy.sum(1, 2) | ||
print(f"1 + 2 = {result}") | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import asyncio | ||
import random | ||
|
||
import pjrpc | ||
from pjrpc.client.backend import aiohttp as pjrpc_client | ||
from pjrpc.client.retry import ExponentialBackoff, PeriodicBackoff, RetryStrategy | ||
|
||
|
||
async def main(): | ||
default_retry_strategy = RetryStrategy( | ||
exceptions={TimeoutError}, | ||
backoff=PeriodicBackoff(attempts=3, interval=1.0, jitter=lambda: random.gauss(mu=0.5, sigma=0.1)), | ||
) | ||
|
||
async with pjrpc_client.Client('http://localhost/api/v1', retry_strategy=default_retry_strategy) as client: | ||
response = await client.send( | ||
pjrpc.Request('sum', params=[1, 2], id=1), | ||
_retry_strategy=RetryStrategy( | ||
exceptions={TimeoutError}, | ||
codes={2001}, | ||
backoff=ExponentialBackoff( | ||
attempts=3, base=1.0, factor=2.0, jitter=lambda: random.gauss(mu=0.5, sigma=0.1), | ||
), | ||
), | ||
) | ||
print(f"1 + 2 = {response.result}") | ||
|
||
result = await client.proxy.sum(1, 2) | ||
print(f"1 + 2 = {result}") | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
"""By default, RabbitMQ JSON-RPC clients generate a temporary result queue | ||
for their requests, but in very special cases, the client may want to choose | ||
a specific result queue. | ||
This example shows using a specific queue with specific properties as well.""" | ||
import asyncio | ||
import logging | ||
|
||
from yarl import URL | ||
|
||
import pjrpc.client.backend.aio_pika | ||
|
||
|
||
async def client_with_specific_queue() -> None: | ||
"""aio_pika client demonstrating the use of a specific result_queue""" | ||
logging.basicConfig(level=logging.INFO, format="%(message)s") | ||
|
||
client = pjrpc.client.backend.aio_pika.Client( | ||
broker_url=URL("amqp://guest:guest@localhost:5672/v1"), | ||
queue_name="jsonrpc", | ||
result_queue_name="pjrpc-aio_pika-example-jsonrpc-results", | ||
result_queue_args={ | ||
"exclusive": True, | ||
"auto_delete": True, | ||
"durable": True, | ||
"arguments": None, | ||
}, | ||
) | ||
await client.connect() | ||
|
||
result = await client.proxy.sum(1, 2) | ||
print(f"1 + 2 = {result}") | ||
|
||
await client.notify("tick") | ||
await client.notify("schedule_shutdown") | ||
await client.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(client_with_specific_queue()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
__description__ = 'Extensible JSON-RPC library' | ||
__url__ = 'https://github.com/dapper91/pjrpc' | ||
|
||
__version__ = '1.5.0' | ||
__version__ = '1.6.0' | ||
|
||
__author__ = 'Dmitry Pershin' | ||
__email__ = '[email protected]' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.