Skip to content

fix: Unify the request pipeline and retry policy across HTTP clients - #1006

Closed
vdusek wants to merge 3 commits into
masterfrom
unify-http-client-pipeline
Closed

fix: Unify the request pipeline and retry policy across HTTP clients#1006
vdusek wants to merge 3 commits into
masterfrom
unify-http-client-pipeline

Conversation

@vdusek

@vdusek vdusek commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Moves call, the retry loop, and the per-attempt request handling from ImpitHttpClient into HttpClient and HttpClientAsync, leaving Impit as a thin adapter over the transport hooks: send_request, is_retryable_transport_error, is_timeout_error, and close() / aclose() plus context managers. A custom client that overrides call keeps working unchanged and opts out of the shared pipeline.

The same pass fixes:

  • Transport errors a retry cannot fix (UnsupportedProtocol, LocalProtocolError, TooManyRedirects) fail on the first attempt instead of burning the whole backoff. Everything else in the impit.HTTPError tree stays retryable, including a bare HTTPError from a body that ends mid-chunk and a ProxyError from a proxy answering CONNECT with a transient status.
  • A failure while reading a streamed error response is classified and retried like a send failure, and the response is closed instead of being left open.
  • HttpResponse comes from typing_extensions, whose runtime protocol check looks attributes up statically, so isinstance(response, HttpResponse) no longer buffers or rejects an unread streaming body.
  • try_import only substitutes failed imports for the optional dependency it names, so an ImportError from the importing module or another dependency is no longer masked, and __all__ omits what a missing extra never provided.

Split out of #1004, which adds the built-in HTTPX client on top of this contract.

✍️ Drafted by Claude Code

Move `call`, the retry loop, and the per-attempt request handling from `ImpitHttpClient` into `HttpClient` and
`HttpClientAsync`, leaving Impit as a thin adapter over the `send_request`, error-classification, and lifecycle
hooks. A custom client that overrides `call` keeps working unchanged and opts out of the shared pipeline.

The same pass fixes:

- Transport errors a retry cannot fix (`UnsupportedProtocol`, `LocalProtocolError`, `TooManyRedirects`) fail on the
  first attempt instead of burning the whole backoff. Every other error in the `impit.HTTPError` tree stays
  retryable, including a bare `HTTPError` from a body that ends mid-chunk and a `ProxyError` from a proxy answering
  CONNECT with a transient status.
- A failure while reading a streamed error response is classified and retried like a send failure, and the response
  is closed instead of being left open.
- `HttpResponse` comes from `typing_extensions`, whose runtime protocol check looks attributes up statically, so
  `isinstance(response, HttpResponse)` no longer buffers or rejects an unread streaming body.
- `try_import` only substitutes failed imports for the optional dependency it names, so an `ImportError` from the
  importing module or another dependency is no longer masked, and `__all__` omits what a missing extra never
  provided.
@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Aug 12, 2026
@vdusek vdusek self-assigned this Aug 12, 2026
@github-actions github-actions Bot added this to the 147th sprint - Tooling team milestone Aug 12, 2026
@github-actions github-actions Bot added the tested Temporary label used only programatically for some analytics. label Aug 12, 2026
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.51244% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.91%. Comparing base (4281530) to head (3d4e037).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/http_clients/_base.py 97.90% 3 Missing ⚠️
src/apify_client/_streamed_log.py 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1006      +/-   ##
==========================================
+ Coverage   94.86%   94.91%   +0.04%     
==========================================
  Files          58       58              
  Lines        5357     5406      +49     
==========================================
+ Hits         5082     5131      +49     
  Misses        275      275              
Flag Coverage Δ
integration 91.62% <68.65%> (-0.66%) ⬇️
unit 85.27% <97.51%> (+0.63%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

vdusek added a commit that referenced this pull request Aug 17, 2026
…dencies (#1009)

`try_import` now swallows only the `ModuleNotFoundError` of the optional
dependency it names, so an import error raised by the importing module
itself or by another dependency propagates instead of being masked.
`__all__` of `apify_client.http_compressors` omits
`BrotliHttpCompressor` when the `brotli` extra is not installed, since
the symbol was never provided.

Split out of #1006.

*✍️ Drafted by Claude Code*
vdusek added a commit that referenced this pull request Aug 17, 2026
…onses (#1010)

`HttpResponse` now comes from `typing_extensions`, whose runtime
protocol check looks attributes up statically. The `typing`
implementation on Python 3.11 calls `hasattr`, which evaluates
properties, so `isinstance(response, HttpResponse)` on an unread
streaming response either raised or silently buffered the whole body.

Adds streaming regression tests (unit and integration) and a
parametrized built-in-client fixture they run on.

Split out of #1006. Stacked on #1009.

*✍️ Drafted by Claude Code*
@vdusek

vdusek commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

This will be split into several smaller PRs

@vdusek vdusek closed this Aug 18, 2026
vdusek added a commit that referenced this pull request Aug 18, 2026
`_utils/errors.py::is_retryable_error` is a stale copy of
`http_clients/_impit.py::_is_retryable_error`. No production code calls
it — only its own tests did — and it sits in a private module, so
nothing depends on it. Once #1019 narrows the real policy, keeping a
second copy that still claims every `impit.HTTPError` is transient is
actively misleading.

Removes the function, its two test cases, and the imports they alone
needed.

Split out of #1006.

*✍️ Drafted by Claude Code*
vdusek added a commit that referenced this pull request Aug 18, 2026
Every `impit.HTTPError` counted as transient, so a transport failure a
retry cannot fix burned the whole backoff before surfacing. The
permanent classes now fail on the first attempt instead:

- `LocalProtocolError` - a request Impit rejects before sending it, e.g.
one carrying an invalid header value.
- `TooManyRedirects` - a routing loop, which repeating the request
cannot break.
- `UnsupportedProtocol` - the class Impit declares for a scheme it
refuses to speak, but does not raise today; an unsupported scheme
arrives as `impit.InvalidURL`, outside the `impit.HTTPError` tree and
non-retryable already. Listed so the classifier stays right if Impit
switches over.
- `HTTPStatusError` - only `Response.raise_for_status()` raises it and
the client never calls it; `_make_request` decides on status codes from
the response itself.

Everything else in the `impit.HTTPError` tree stays retryable, including
a bare `HTTPError` - Impit wraps a failure its internal HTTP library did
not classify in one, e.g. a non-HTTP response or a connection reset -
and a `ProxyError`, which covers a rejected CONNECT tunnel and a 407
alike, so a transient case cannot be told from a permanent one.

Also drops `InvalidResponseBodyError` from the retry check. Only
`key_value_store.py` raises it, once `call` has already returned, so it
can never reach the classifier. Its docstring no longer claims the
client retries such requests. The duplicate `is_retryable_error` in
`_utils/errors.py`, which carried the old policy, is gone.

Split out of #1006 and re-targeted at `master`, so the retry fixes can
ship in a patch release ahead of the pipeline refactor in #1011.

*✍️ Drafted by Claude Code*
vdusek added a commit that referenced this pull request Aug 18, 2026
…1020)

Before raising `ApifyApiError`, `_make_request` buffers the response so
a streamed error body reaches the exception. That read sat outside the
`try` block that classifies an attempt, so a failure there escaped the
retry policy entirely: a non-retryable failure was retried through the
whole backoff, and the response was left open.

The read now goes through the same classification as a failed send —
retried when it is a transient transport failure, surfaced immediately
when it is not — and the response is closed before the exception
propagates.

Split out of #1006 and re-targeted at `master`, so the retry fixes can
ship in a patch release ahead of the pipeline refactor in #1011.

*✍️ Drafted by Claude Code*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants