Skip to content

Commit

Permalink
Del RecoverableServerError, del retry middleware (#56)
Browse files Browse the repository at this point in the history
* Del RecoverableServerError, del retry middleware

* Add comment

* Set version 0.3.25
  • Loading branch information
evgeny-stakewise authored Sep 11, 2023
1 parent 67203b5 commit 34047e0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sw-utils"
version = "0.3.24"
version = "0.3.25"
description = "StakeWise Python utils"
authors = ["StakeWise Labs <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down
13 changes: 9 additions & 4 deletions sw_utils/consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from web3.types import Timestamp

from sw_utils.common import urljoin
from sw_utils.decorators import retry_aiohttp_errors
from sw_utils.exceptions import AiohttpRecoveredErrors
from sw_utils.decorators import can_be_retried_aiohttp_error, retry_aiohttp_errors
from sw_utils.typings import ChainHead, ConsensusFork

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -91,7 +90,10 @@ async def submit_voluntary_exit(
response.raise_for_status()
return

except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self.base_urls) - 1:
raise error
logger.error('%s: %s', url, repr(error))
Expand Down Expand Up @@ -152,7 +154,10 @@ async def _async_make_get_request_inner(self, endpoint_uri: str) -> dict[str, An
uri = URI(urljoin(url, endpoint_uri))
return await async_json_make_get_request(uri, timeout=self.timeout)

except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self.base_urls) - 1:
raise error
logger.error('%s: %s', url, repr(error))
Expand Down
28 changes: 1 addition & 27 deletions sw_utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,12 @@
logger = logging.getLogger(__name__)


class RecoverableServerError(Exception):
"""
Wrapper around ClientResponseError for HTTP 500 errors.
Only for internal use inside sw-utils library.
Do not raise `RecoverableServerError` in application code.
"""

def __init__(self, origin: requests.HTTPError | aiohttp.ClientResponseError):
self.origin = origin
if isinstance(origin, requests.HTTPError):
self.status_code = origin.response.status_code
self.uri = origin.response.url
elif isinstance(origin, aiohttp.ClientResponseError):
self.status_code = origin.status
self.uri = origin.request_info

super().__init__()

def __str__(self):
return (
f'RecoverableServerError (status_code: {self.status_code}, '
f'uri: {self.uri}): {self.origin}'
)


# DEPRECATED
AiohttpRecoveredErrors = (
aiohttp.ClientConnectionError,
RecoverableServerError,
asyncio.TimeoutError,
)
RequestsRecoveredErrors = (
requests.ConnectionError,
requests.Timeout,
RecoverableServerError,
)
14 changes: 10 additions & 4 deletions sw_utils/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
from web3.middleware import async_geth_poa_middleware
from web3.net import AsyncNet
from web3.providers.async_rpc import AsyncHTTPProvider
from web3.types import RPCEndpoint, RPCResponse, Wei
from web3.types import AsyncMiddleware, RPCEndpoint, RPCResponse, Wei

from sw_utils.decorators import retry_aiohttp_errors
from sw_utils.exceptions import AiohttpRecoveredErrors
from sw_utils.decorators import can_be_retried_aiohttp_error, retry_aiohttp_errors

logger = logging.getLogger(__name__)

Expand All @@ -35,6 +34,9 @@ class ExtendedAsyncHTTPProvider(AsyncHTTPProvider):
_providers: list[AsyncHTTPProvider] = []
_locker_provider: AsyncHTTPProvider | None = None

# Turn off `async_http_retry_request_middleware`
_middlewares: tuple[AsyncMiddleware, ...] = ()

def __init__(
self,
endpoint_urls: list[str],
Expand Down Expand Up @@ -83,9 +85,13 @@ async def make_request_inner(self, method: RPCEndpoint, params: Any) -> RPCRespo
try:
response = await provider.make_request(method, params)
return response
except AiohttpRecoveredErrors as error:
except Exception as error:
if not can_be_retried_aiohttp_error(error):
raise error

if i == len(self._providers) - 1:
raise error

logger.error('%s: %s', provider.endpoint_uri, repr(error))

return {}
Expand Down

0 comments on commit 34047e0

Please sign in to comment.