Add HTTP/2 PING keepalive to prevent idle connection timeouts#1081
Open
balaji-g wants to merge 1 commit intoencode:masterfrom
Open
Add HTTP/2 PING keepalive to prevent idle connection timeouts#1081balaji-g wants to merge 1 commit intoencode:masterfrom
balaji-g wants to merge 1 commit intoencode:masterfrom
Conversation
43fa25f to
da08e37
Compare
Many cloud load balancers and proxies (e.g. AWS Global Accelerator) terminate idle TCP connections after a fixed timeout (often 340s). HTTP/2 PING frames (RFC 9113 §6.7) reset this idle timer, but httpcore does not currently send them. This adds a background thread that sends periodic HTTP/2 PING frames on active connections, configurable via: - `h2_ping_interval` parameter on ConnectionPool / HTTPConnection - `HTTPCORE_H2_PING_INTERVAL` environment variable (seconds) The env var allows enabling PING keepalive without any code changes, which is critical for users of higher-level clients like httpx and the OpenAI Python SDK that don't expose httpcore internals. Closes encode#1080 Made-with: Cursor
da08e37 to
2b668b2
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.
Summary
Many cloud load balancers and network proxies terminate idle TCP connections after a fixed timeout. HTTP/2 PING frames (RFC 9113 §6.7) are the standard mechanism to keep connections alive, but httpcore does not currently send them — meaning any HTTP/2 request that takes longer than the idle timeout will fail with a disconnect error.
This adds a background keepalive thread that sends periodic HTTP/2 PING frames on active connections. It can be enabled via:
h2_ping_intervalonConnectionPool/HTTPConnection/HTTP2ConnectionHTTPCORE_H2_PING_INTERVAL(value in seconds)The env var approach is particularly important because it allows users of higher-level clients (httpx, OpenAI Python SDK) to enable PING keepalive without any code changes.
Example
Design decisions
threading.Event.wait()for the interval, avoiding interference with the async event loop.threading.Lockto avoid races with the main h2 state machine.HTTPCORE_H2_PING_INTERVALis read at connection init time. An explicit constructor value always takes precedence.close()/aclose().Related issue: #1080
Checklist