Repository navigation
fix(async): use httpx default keep-alive and add a limits argument - #256
Merged
Merged
Conversation
_build_unit_stats_endpoint concatenated `language` into the stats query
string unchanged, so a value such as "eng_us&flags=onlyGP#x" could add
query parameters or a fragment to the request.
- A value that is not a language code (^[A-Za-z]{2,3}_[A-Za-z]{2}$, full
match) now raises SwgohComlinkValueError before any request is sent.
- A well-formed code not in Constants.LANGUAGES (case-insensitive) logs
one warning and is still sent unchanged.
- The query is built with urlencode(..., safe=","), so the output for
valid input is byte-identical (api?flags=calcGP&language=eng_us) and
the HMAC-signed path still matches the path on the wire.
Shared code in _base.py, so both clients are covered; tests run against
the sync and async clients.
Refs #235
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…T_KEY Both clients fell back to the generic ACCESS_KEY / SECRET_KEY environment variables when no keys were passed. If another tool set those names, HMAC turned on silently and that tool's access key went to Comlink in the Authorization header. Credentials now resolve as: constructor arguments, then COMLINK_ACCESS_KEY / COMLINK_SECRET_KEY, then (only when neither new name is set) the old ACCESS_KEY / SECRET_KEY names. Using an old name emits one DeprecationWarning per client naming the new variables. When only one key of the pair ends up set, a warning is logged since HMAC stays off. The change is in SwgohComlinkBase, so it covers SwgohComlink and SwgohComlinkAsync alike; tests run against both. Refs #236 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HMAC signing authenticates requests but does not protect responses or stop replays within the server's X-Date window. Over http:// to a remote host, both are open to anyone on the network path, and the client accepted that setup silently. When HMAC is on, each client now logs one warning naming its url and/or stats_url that use plain http to a host other than localhost, 127.0.0.0/8 or ::1 (literal IPs checked with ipaddress; host names are not resolved). The connection is never blocked. docs/index.md gains a paragraph recommending TLS or a private network for a remote Comlink. The check is in SwgohComlinkBase, so it covers SwgohComlink and SwgohComlinkAsync alike; tests run against both. Refs #237 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…imits Both clients accept two keyword-only constructor arguments, defaulting to None (no limit, and the existing buffered request path unchanged): - max_response_bytes applies to every endpoint except /data, including the stats service. - max_game_data_bytes applies to /data (get_game_data), whose responses are far larger, mirroring DEFAULT_TIMEOUT / GAME_DATA_TIMEOUT. When the applicable limit is set, _send streams the response. A success whose Content-Length is over the limit raises SwgohComlinkValueError before the body is read; otherwise decoded bytes from iter_bytes() are counted and the read aborts with SwgohComlinkValueError past the limit, which also stops gzip bombs. An error body is read only up to the limit and cut there before SwgohComlinkHTTPError.from_response, so status classification and retries are unchanged. Adds a UTF-8 BOM regression test: a BOM-prefixed body still raises SwgohComlinkDecodeError, buffered and streamed. Refs #240 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SwgohComlinkAsync built its httpx clients with httpx.Limits(keepalive_expiry=None), so idle pooled connections were never closed, while SwgohComlink used the httpx default of 5 s. A stale connection after a Comlink restart or NAT idle timeout could then fail the next call with RemoteProtocolError. Remove the async override so both clients use the httpx default pool limits, and add a keyword-only limits: httpx.Limits | None = None argument to both constructors. None passes no limits to httpx, keeping its defaults; a value is passed to every httpx client both classes build. Request timeouts are unchanged. Refs #241 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 tasks done
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.
Task: PERF-2 (v2.9 hardening plan, PR 6)
Branch:
fix/keepalive-parityCloses #241
What changed
SwgohComlinkAsyncno longer builds its httpx clients withhttpx.Limits(keepalive_expiry=None). Both clients now use the httpx default pool limits (100 connections, 20 kept alive, idle connections closed after 5 s), so a connection left stale by a Comlink restart or a NAT idle timeout is no longer reused indefinitely.limits: httpx.Limits | None = Noneargument on bothSwgohComlinkandSwgohComlinkAsync(declared once inSwgohComlinkBase.__init__, stored asself.limits).Nonepasses nolimitsto httpx, so the httpx defaults apply. A value is passed to every httpx client (clientandstats_client) in both classes through one shared_pool_limitsmapping, so the two clients cannot drift apart again. Anything other than anhttpx.LimitsorNoneraisesSwgohComlinkTypeError, asretry=does.DEFAULT_TIMEOUT(120 s) andGAME_DATA_TIMEOUT(300 s) for/data.limits.docs/migration.mdhas a v2.9 entry for the async keep-alive change.Acceptance
tests/unit/test_connection_limits.py::test_default_keepalive_expiry_matches_httpx_default_in_both_clientschecks the connection pools behindclientandstats_clientin both classes against a plainhttpx.Client(): 5.0 s keep-alive, 100 / 20 connections. The issue's repro now prints5.0 5.0(it printed5.0 Nonebefore).limitsto httpx.test_sync_and_async_pass_identical_limits_to_httpx[default|custom]records the keyword arguments given tohttpx.Client/httpx.AsyncClient. With the default, neither class passeslimits. With a customhttpx.Limits, both pass that same object to both of their clients.test_custom_limits_reach_both_connection_poolschecks that a custom value reaches the httpcore pools. Further tests cover keyword-only, type validation and unchanged timeouts. All 14 failed before the fix and pass after it.docs/migration.mdentry for the async keep-alive change. It is under "10. v2.9 changes" and giveslimits=httpx.Limits(keepalive_expiry=None)to get the old async behaviour back.Verification:
uv run ruff check src/ tests/ examples/ parity/: passeduv run ruff format --check src/ tests/ examples/ parity/: passeduv run ty check src/swgoh_comlink/: passeduv run pytest tests/unit -q: 1801 passedMigration notes
SwgohComlinkAsyncnow closes idle pooled connections after 5 s, asSwgohComlinkalready did. To keep the old behaviour, useSwgohComlinkAsync(limits=httpx.Limits(keepalive_expiry=None)). Nothing else changes for callers who do not passlimits.If stale-connection failures continue after this change, retrying transport errors (
httpx.RequestError) should get its own issue, because it changes the documented retry contract.🤖 Generated with Claude Code