Repository navigation
fix: retry transient network errors #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import asyncio | ||
| import io | ||
| import json | ||
| import logging | ||
|
|
@@ -23,6 +24,12 @@ | |
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| NETWORK_ERRORS = ( | ||
| aiohttp.ClientConnectionError, | ||
| aiohttp.ClientPayloadError, | ||
| asyncio.TimeoutError, | ||
| ) | ||
|
|
||
|
|
||
| class RESTResponse(io.IOBase): | ||
| """ | ||
|
|
@@ -329,14 +336,18 @@ async def stream( | |
| Streams JSON objects from a specified endpoint, handling partial chunks | ||
| and leftover data at the end of the stream. | ||
|
|
||
| The request is sent and the status checked before returning, so errors | ||
| raise here. The returned iterator holds the connection until it is | ||
| exhausted or closed. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing the REST iterator works, but closing the public stream after one item still leaves the connection checked out. Both |
||
|
|
||
| :param method: The HTTP method (GET, POST, etc.). | ||
| :param url: The endpoint URL. | ||
| :param query_params: Query parameters to be appended to the URL. | ||
| :param headers: Optional headers to include in the request. | ||
| :param body: Optional body for the request. | ||
| :param post_params: Optional form/multipart parameters. | ||
| :param _request_timeout: An optional request timeout in seconds. | ||
| :yields: Parsed JSON objects as Python data structures. | ||
| :return: An async iterator of parsed JSON objects. | ||
| """ | ||
|
|
||
| # Build our request payload | ||
|
|
@@ -351,36 +362,41 @@ async def stream( | |
| _request_timeout=_request_timeout, | ||
| ) | ||
|
|
||
| # Send the request and check its status before returning the stream | ||
| stream = self._stream(args) | ||
| await anext(stream) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The returned generator is already advanced. If the caller never iterates or closes it, the response is only released on GC. Document that it must be consumed or closed, or wrap it with |
||
| return stream | ||
|
|
||
| async def _stream(self, args: dict): | ||
| """ | ||
| Sends the request and yields once its status is checked, then yields | ||
| JSON objects from the response and releases it. | ||
| """ | ||
|
|
||
| # Send request, collect response handler | ||
| response = await self.pool_manager.request(**args) | ||
|
|
||
| # Initialize buffers for data chunks | ||
| buffer = bytearray() | ||
| leftover = b"" | ||
| response: aiohttp.ClientResponse | None = None | ||
|
|
||
| try: | ||
| # Send request, collect response handler | ||
| async with self.pool_manager.request(**args) as resp: | ||
| response = resp | ||
| try: | ||
| # Iterate over streamed/chunked response data | ||
| async for data, _ in resp.content.iter_chunks(): | ||
| if data: | ||
| # Process data chunk | ||
| leftover, decoded_objects = self._accumulate_json_lines( | ||
| leftover, data, buffer | ||
| ) | ||
|
|
||
| # Yield any complete objects | ||
| for obj in decoded_objects: | ||
| yield obj | ||
|
|
||
| except Exception as e: | ||
| logger.exception("Stream reading error: %s", e) | ||
|
|
||
| except Exception as conn_err: | ||
| logger.exception("Connection or request setup error: %s", conn_err) | ||
|
|
||
| # Handle any remaining data after stream ends | ||
| if response is not None: | ||
| # Handle any HTTP errors that may have occurred | ||
| await self.handle_response_exception(response) | ||
| yield | ||
|
|
||
| # Iterate over streamed/chunked response data | ||
| async for data, _ in response.content.iter_chunks(): | ||
| if data: | ||
| # Process data chunk | ||
| leftover, decoded_objects = self._accumulate_json_lines( | ||
| leftover, data, buffer | ||
| ) | ||
|
|
||
| # Yield any complete objects | ||
| for obj in decoded_objects: | ||
| yield obj | ||
|
|
||
| # Check for any leftover data | ||
| if leftover: | ||
| try: | ||
|
|
@@ -396,16 +412,8 @@ async def stream( | |
| # Decode the complete/buffered data for logging purposes | ||
| if isinstance(response, aiohttp.ClientResponse): | ||
| logger.debug("response body: %s", buffer.decode("utf-8")) | ||
|
|
||
| try: | ||
| # Handle any HTTP errors that may have occurred | ||
| await self.handle_response_exception(response) | ||
| finally: | ||
| # Release the response object back to the connection pool. | ||
| # This must always run, even if handle_response_exception raises, | ||
| # to avoid leaking the connection (preload_content=False means | ||
| # the connection is not auto-released). | ||
| response.release() | ||
| finally: | ||
| response.release() | ||
|
|
||
| async def request( | ||
| self, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documented aiohttp replay still bypasses the per-call retry budget in #342. With aiohttp 3.14.5, a dropped async GET sent two requests and succeeded even with
RetryParams(max_retry=0). Disable aiohttp's replay for SDK-managed requests and add an async GET regression test that asserts exactly one request with retries disabled. The current async exhaustion test only covers POST.