Respect HTTP/2 stream capacity in connection pool#1088
Open
sarmientoF wants to merge 1 commit into
Open
Conversation
17c04e8 to
8e904f6
Compare
Queue or open another connection when existing HTTP/2 connections are at the remote stream cap instead of assigning requests to full connections.
8e904f6 to
bd2e06e
Compare
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.
What changed
HTTP/2 connections can have spare TCP capacity but no spare stream capacity. Today the pool can still hand another request to that connection. With a low peer
SETTINGS_MAX_CONCURRENT_STREAMS, that turns into a stall orTooManyStreamsErroreven whenmax_connectionswould allow another connection.This patch makes the pool count active requests per connection and compare that with the connection's stream capacity. When all same-origin HTTP/2 connections are full, the pool either opens another connection or leaves the request queued until capacity comes back.
It also makes the HTTP/2 stream reservation fail fast. If there is a race between the pool check and the connection check, the connection raises
ConnectionNotAvailableand the pool can retry or queue the request instead of blocking inside the semaphore.Fixes #248.
Related E2B context: e2b-dev/E2B#1407 added a Python SDK HTTP/2 opt-out, but the underlying failure mode is in httpcore's H2 pool behavior. That is the behavior this PR fixes at the transport layer.
Local repro
I used this gist for the local repro and timing table:
https://gist.github.com/sarmientoF/32bc18a587d96590cece3ecf9f4719dd
The script starts a local h2c server with
SETTINGS_MAX_CONCURRENT_STREAMS=2, opens two long-lived responses, then sends a third request.mastermax_connections=3200in ~1.7ms, 2 server connectionsmax_connections=3200in ~4.1ms, 2 server connectionsmax_connections=1PoolTimeoutat ~502ms, next request200in ~0.7msmax_connections=1PoolTimeoutat ~501ms, next request200in ~1.2msTooManyStreamsErrororLocalProtocolErrorTooManyStreamsErrororLocalProtocolErrorCancelledError, next request200in ~1.0msThe timings are from one local macOS/Python 3.12 run. I would not treat them as benchmark claims. They are mostly useful for showing the old failure mode and the new queue/open behavior.
How to run it
Set up two worktrees:
git clone https://github.com/encode/httpcore.git httpcore-master git clone https://github.com/sarmientoF/httpcore.git httpcore-pr (cd httpcore-pr && git checkout fix-http2-stream-cap-pooling)Download the repro script:
Run it against
master:Run it against this branch:
There is also a small throughput loop:
That loop uses
SETTINGS_MAX_CONCURRENT_STREAMS=100and sequential requests. It is a smoke test for normal reuse overhead, not the main proof for this bug.Checks
python -m pytest232 passed, 4 xfailed, 2 xpassedpython scripts/unasync.py --checkpython -m ruff check httpcore testspython -m ruff format httpcore tests --diffpython -m mypy httpcore tests/_async/test_http2.py tests/_sync/test_http2.py tests/_async/test_connection_pool.py tests/_sync/test_connection_pool.py tests/test_cancellations.pygit diff --check