Repository navigation
perf: count capped bytes on the stream for unencoded bodies - #192
Merged
Merged
Conversation
For a response with no Content-Encoding, the body cap now wraps response.stream in a stream that counts bytes and raises ResponseTooLargeError once the count passes the cap, and httpx2 reads the response with its own read()/aread(). The capped response is the object httpx2 built, so its headers, extensions and num_bytes_downloaded match an uncapped one. Encoded bodies keep the rebuild path. Closes #170
httpx2 2.0.0 closes a response only when its body is read to the end, so an over-cap read left it open on the floor version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For a capped response with no
Content-Encoding, the decoded bytes are the wire bytes._read_capped/_read_capped_asyncnow wrapresponse.streamin_CappedSyncStream/_CappedAsyncStream, which count bytes and raiseResponseTooLargeError(reason="streamed")once the count passes the cap, and let httpx2 read the response with its ownread()/aread(). The capped response is the object httpx2 built, so nothing is copied. Bodies with anyContent-Encoding(includingidentity) keep the rebuild path, as ADR-0009 requires.Everything stays inside
_internal/body_cap.py;client.pyis unchanged. Every caller (the terminal, the authrequires_response_bodypath, thestream()error pre-read) goes through the two functions.How it uses httpx2
Public API only:
Response.stream,httpx2.SyncByteStreamandhttpx2.AsyncByteStream. I read httpx2 2.13.1 to confirm the details:iter_raw/aiter_rawclose the response in afinally. On the 2.0.0 floor they close it only when the body is read to the end, so an error mid-read leaves it open. The fast path therefore closes the response itself whenread()/aread()raises, and a cap trip closes the response and the wrapped transport stream on every supported httpx2.close()/aclose()are idempotent in both versions, so the callers' ownfinallycloses still work. The new tests check this with a stream that records its close.Response.elapsedon a streamed response readsgetattr(self.stream, "elapsed", None)fromBoundSyncStream/BoundAsyncStream, so the wrappers forwardelapsed. On 2.0.0,BoundSyncStream.close()setsresponse.elapseditself, and the wrapper'sclose()delegates to it. The elapsed andnum_bytes_downloadedtests pass on both.finally, the same wayBoundAsyncStreamdoes.The declared
Content-Lengthcheck still runs first and raisesreason="declared"before any byte is read. The error'sstatus_code,limit,content_lengthandreasonare unchanged.Behaviour changes (unencoded bodies, cap set)
num_bytes_downloadedis the real byte count, the same as without a cap. It used to be0.Transfer-Encoding: chunked; it used to be dropped and aContent-Lengthadded.Content-Length, when sent, is unchanged because it already equals the body size.extensionskeepsnetwork_stream, as an uncapped response does. The rebuild dropped it.Encoded bodies behave as before.
docs/errors.mdand ADR-0009 (updated in place) describe both paths.Tests
Written first and failing before the change:
num_bytes_downloadedset (sync and async)num_bytes_downloaded == 0reason="streamed"and closes the response and its streamnum_bytes_downloadedmatches with and without a capjust lint-ciandjust test-cipass (1352 tests, 100% coverage) after rebasing onto 660146e. The full suite also passes on a local floors install (httpx2 2.0.0, Python 3.14) built the way thefloorsjob builds it.Benchmark
Capped
get()(max_response_body_bytes=1_000_000) against aMockTransportwith a ~1 KB JSON body, best of 7 x 5000 calls, before and after runs interleaved in one session, two rounds:The machine was under load, so absolute numbers run high, but earlier rounds under heavier load showed the same ratio (sync unencoded 158-183 µs before, 122-128 µs after). The gzip path is unchanged within noise.
Closes #170