Skip to content

fix(async): use httpx default keep-alive and add a limits argument - #256

Merged
MarTrepodi merged 5 commits into
developfrom
fix/keepalive-parity
Nov 7, 2026
Merged

MarTrepodi merged 5 commits into
developfrom
fix/keepalive-parity

Conversation

@MarTrepodi

Copy link
Copy Markdown
Collaborator

Stacked PR. Depends on #255 (fix/response-size-cap); merge it first. Until then this diff also shows the parent commits. To review only this PR's changes: fix/response-size-cap...fix/keepalive-parity.

Task: PERF-2 (v2.9 hardening plan, PR 6)
Branch: fix/keepalive-parity

Closes #241

What changed

  • SwgohComlinkAsync no longer builds its httpx clients with httpx.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.
  • New keyword-only limits: httpx.Limits | None = None argument on both SwgohComlink and SwgohComlinkAsync (declared once in SwgohComlinkBase.__init__, stored as self.limits). None passes no limits to httpx, so the httpx defaults apply. A value is passed to every httpx client (client and stats_client) in both classes through one shared _pool_limits mapping, so the two clients cannot drift apart again. Anything other than an httpx.Limits or None raises SwgohComlinkTypeError, as retry= does.
  • Request timeouts are unchanged: DEFAULT_TIMEOUT (120 s) and GAME_DATA_TIMEOUT (300 s) for /data.
  • Docstrings for both clients document limits. docs/migration.md has a v2.9 entry for the async keep-alive change.

Acceptance

  • Both clients build their httpx clients with identical limits. tests/unit/test_connection_limits.py::test_default_keepalive_expiry_matches_httpx_default_in_both_clients checks the connection pools behind client and stats_client in both classes against a plain httpx.Client(): 5.0 s keep-alive, 100 / 20 connections. The issue's repro now prints 5.0 5.0 (it printed 5.0 None before).
  • A unit test asserts that sync and async pass the same limits to httpx. test_sync_and_async_pass_identical_limits_to_httpx[default|custom] records the keyword arguments given to httpx.Client / httpx.AsyncClient. With the default, neither class passes limits. With a custom httpx.Limits, both pass that same object to both of their clients. test_custom_limits_reach_both_connection_pools checks 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.
  • A docs/migration.md entry for the async keep-alive change. It is under "10. v2.9 changes" and gives limits=httpx.Limits(keepalive_expiry=None) to get the old async behaviour back.

Verification:

  • uv run ruff check src/ tests/ examples/ parity/: passed
  • uv run ruff format --check src/ tests/ examples/ parity/: passed
  • uv run ty check src/swgoh_comlink/: passed
  • uv run pytest tests/unit -q: 1801 passed

Migration notes

SwgohComlinkAsync now closes idle pooled connections after 5 s, as SwgohComlink already did. To keep the old behaviour, use SwgohComlinkAsync(limits=httpx.Limits(keepalive_expiry=None)). Nothing else changes for callers who do not pass limits.

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

MarTrepodi and others added 5 commits November 7, 2026 08:24
_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>
@github-actions github-actions Bot added documentation Improvements or additions to documentation code testing labels Nov 7, 2026
@MarTrepodi
MarTrepodi merged commit 433b764 into develop Nov 7, 2026
12 checks passed
@MarTrepodi
MarTrepodi deleted the fix/keepalive-parity branch November 7, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code documentation Improvements or additions to documentation testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant