-
Notifications
You must be signed in to change notification settings - Fork 4
feat: retire idle keep-alive connections after 2s to avoid stale-socket ReadError (OPS-4976) #78
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
Open
doctorcorral
wants to merge
3
commits into
main
Choose a base branch
from
corral/OPS-4976-keepalive-expiry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c2e649e
feat: retire idle keep-alive connections after 2s (PYGQLC_KEEPALIVE_E…
doctorcorral adb2674
fix: thread keepalive limits into ipv4_only transport + validate env var
doctorcorral 56ddce2
Merge origin/main; rebase keepalive change to 3.8.7 (on top of 3.8.5/…
doctorcorral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "3.8.4" | ||
| __version__ = "3.8.5" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Idle keep-alive connections are retired quickly via httpx ``keepalive_expiry`` | ||
| so a socket the server already closed is never reused — the stale keep-alive that | ||
| surfaces as ``ReadError('')``. Default 2s, tunable via ``PYGQLC_KEEPALIVE_EXPIRY``. | ||
| """ | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from pygqlc import GraphQLClient | ||
| from pygqlc.GraphQLClient import DEFAULT_KEEPALIVE_EXPIRY | ||
| from pygqlc.helper_modules.Singleton import Singleton | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fresh_client(): | ||
| """Build GraphQLClient instances whose ``__init__`` re-reads the env, without | ||
| leaking state into the process-wide singleton other tests share.""" | ||
| saved = Singleton._instances.pop(GraphQLClient, None) | ||
|
|
||
| def _build(): | ||
| Singleton._instances.pop(GraphQLClient, None) | ||
| return GraphQLClient() | ||
|
|
||
| yield _build | ||
|
|
||
| Singleton._instances.pop(GraphQLClient, None) | ||
| if saved is not None: | ||
| Singleton._instances[GraphQLClient] = saved | ||
|
|
||
|
|
||
| def test_default_keepalive_expiry_on_both_clients(fresh_client, monkeypatch): | ||
| monkeypatch.delenv("PYGQLC_KEEPALIVE_EXPIRY", raising=False) | ||
| gql = fresh_client() | ||
| for params in (gql.client_params, gql.async_client_params): | ||
| limits = params["limits"] | ||
| assert isinstance(limits, httpx.Limits) | ||
| assert limits.keepalive_expiry == DEFAULT_KEEPALIVE_EXPIRY | ||
| # httpx's standard pool caps must be preserved, not reset to None. | ||
| assert limits.max_connections == 100 | ||
| assert limits.max_keepalive_connections == 20 | ||
|
|
||
|
|
||
| def test_keepalive_expiry_env_override(fresh_client, monkeypatch): | ||
| monkeypatch.setenv("PYGQLC_KEEPALIVE_EXPIRY", "0.5") | ||
| gql = fresh_client() | ||
| assert gql.client_params["limits"].keepalive_expiry == 0.5 | ||
| assert gql.async_client_params["limits"].keepalive_expiry == 0.5 | ||
|
|
||
|
|
||
| def test_default_below_httpx_default(): | ||
| # The point of the mitigation: shorter than httpx's 5.0s default so a | ||
| # server/LB with a shorter idle timeout can't close the socket first. | ||
| assert DEFAULT_KEEPALIVE_EXPIRY < 5.0 | ||
|
doctorcorral marked this conversation as resolved.
Outdated
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.