Skip to content

Commit

Permalink
Retrying on aiohttp.ClientConnectionResetError (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza authored Oct 4, 2024
1 parent caca42a commit 81ef8a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions paperqa/clients/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def __init__(self, message="DOI not found") -> None:

def make_flaky_ssl_error_predicate(host: str) -> Callable[[BaseException], bool]:
def predicate(exc: BaseException) -> bool:
# Seen with both Semantic Scholar and Crossref:
# > aiohttp.client_exceptions.ClientConnectorError:
# > Cannot connect to host api.host.org:443 ssl:default [nodename nor servname provided, or not known]
# SEE: https://github.com/aio-libs/aiohttp/blob/v3.10.5/aiohttp/client_exceptions.py#L193-L196
Expand Down
25 changes: 16 additions & 9 deletions paperqa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,23 @@ def create_bibtex_key(author: list[str], year: str, title: str) -> str:
return remove_substrings(key, FORBIDDEN_KEY_CHARACTERS)


def is_retryable(exc: BaseException) -> bool:
"""Check if an exception is known to be a retryable HTTP issue."""
if isinstance(
exc, aiohttp.ServerDisconnectedError | aiohttp.ClientConnectionResetError
):
# Seen with Semantic Scholar:
# > aiohttp.client_exceptions.ClientConnectionResetError:
# > Cannot write to closing transport
return True
return isinstance(exc, aiohttp.ClientResponseError) and exc.status in {
httpx.codes.INTERNAL_SERVER_ERROR.value,
httpx.codes.GATEWAY_TIMEOUT.value,
}


@retry(
retry=retry_if_exception(
lambda x: isinstance(x, aiohttp.ServerDisconnectedError)
or isinstance(x, aiohttp.ClientResponseError)
and x.status
in {
httpx.codes.INTERNAL_SERVER_ERROR.value,
httpx.codes.GATEWAY_TIMEOUT.value,
}
),
retry=retry_if_exception(is_retryable),
before_sleep=before_sleep_log(logger, logging.WARNING),
stop=stop_after_attempt(5),
wait=wait_incrementing(0.1, 0.1),
Expand Down

0 comments on commit 81ef8a3

Please sign in to comment.