Include new methods in retry-able methods

This commit is contained in:
Luke Van Seters 2021-12-25 17:23:21 -05:00
parent fcc453391f
commit 7707b818f0

View File

@ -5,6 +5,7 @@ from time import sleep
from typing import Any, Callable, Collection, Coroutine, Type from typing import Any, Callable, Collection, Coroutine, Type
from aiohttp.client_exceptions import ( from aiohttp.client_exceptions import (
ClientConnectorError,
ClientOSError, ClientOSError,
ClientResponseError, ClientResponseError,
ServerDisconnectedError, ServerDisconnectedError,
@ -12,20 +13,33 @@ from aiohttp.client_exceptions import (
) )
from requests.exceptions import ConnectionError, HTTPError, Timeout, TooManyRedirects from requests.exceptions import ConnectionError, HTTPError, Timeout, TooManyRedirects
from web3 import Web3 from web3 import Web3
from web3.middleware.exception_retry_request import check_if_retry_on_failure from web3.middleware.exception_retry_request import whitelist
from web3.types import RPCEndpoint, RPCResponse from web3.types import RPCEndpoint, RPCResponse
request_exceptions = (ConnectionError, HTTPError, Timeout, TooManyRedirects) request_exceptions = (ConnectionError, HTTPError, Timeout, TooManyRedirects)
aiohttp_exceptions = ( aiohttp_exceptions = (
ClientOSError, ClientOSError,
ClientResponseError,
ClientConnectorError,
ServerDisconnectedError, ServerDisconnectedError,
ServerTimeoutError, ServerTimeoutError,
ClientResponseError,
) )
whitelist_additions = ["eth_getBlockReceipts", "trace_block", "eth_feeHistory"]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def check_if_retry_on_failure(method: RPCEndpoint) -> bool:
root = method.split("_")[0]
if root in (whitelist + whitelist_additions):
return True
elif method in (whitelist + whitelist_additions):
return True
else:
return False
async def exception_retry_with_backoff_middleware( async def exception_retry_with_backoff_middleware(
make_request: Callable[[RPCEndpoint, Any], Any], make_request: Callable[[RPCEndpoint, Any], Any],
web3: Web3, # pylint: disable=unused-argument web3: Web3, # pylint: disable=unused-argument
@ -47,7 +61,7 @@ async def exception_retry_with_backoff_middleware(
# https://github.com/python/mypy/issues/5349 # https://github.com/python/mypy/issues/5349
except errors: # type: ignore except errors: # type: ignore
logger.error( logger.error(
f"Request for method {method}, block: {int(params[0], 16)}, retrying: {i}/{retries}" f"Request for method {method}, params: {params}, retrying: {i}/{retries}"
) )
if i < (retries - 1): if i < (retries - 1):
backoff_time = backoff_time_seconds * ( backoff_time = backoff_time_seconds * (
@ -72,5 +86,9 @@ async def http_retry_with_backoff_request_middleware(
return await exception_retry_with_backoff_middleware( return await exception_retry_with_backoff_middleware(
make_request, make_request,
web3, web3,
(request_exceptions + aiohttp_exceptions + (TimeoutError,)), (
request_exceptions
+ aiohttp_exceptions
+ (TimeoutError, ConnectionRefusedError)
),
) )