Skip to content

fix(client): unrestricted Swoole clients leave name resolution to Swoole - #14066

Merged
loks0n merged 6 commits into
mainfrom
fix-swoole-anywhere-resolution
Oct 2, 2026
Merged

loks0n merged 6 commits into
mainfrom
fix-swoole-anywhere-resolution

Conversation

@loks0n

@loks0n loks0n commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Follow-up to #13998. It fixes a regression that PR introduced in the Swoole HTTP adapter: clients with no destination restriction started resolving hostnames themselves, which made a failed lookup slow.

The problem

#13998 gave every client Destinations. To enforce them, the Swoole adapter resolves the hostname itself (getaddrinfo() for A and AAAA), checks the addresses, and dials one that is allowed. It did that for every client, including Anywhere, the default, where there is nothing to check.

So every unrestricted Swoole client (the geo client, the pooled internal clients) got a different resolution path for no benefit: two getaddrinfo() lookups per new connection instead of Swoole's own resolver.

The cost shows up where a failed lookup is slow. In a cluster whose lookups fall through the search domains to a slow upstream resolver, a request to a service that isn't deployed waited out the connect timeout instead of failing at once. A downstream build that calls a geo service on every request never passed its startup probe, because each health check took the full 5 seconds.

The fix

An Anywhere client hands the hostname straight to Swoole, as it did before Destinations existed, and reconnects the same way. Restricted clients are unchanged: they still resolve, check and dial an allowed address.

Two things the adapter's own resolution used to do for IPv6 are kept on the unrestricted path:

  • IPv6 literals. The address is dialed without the URI's brackets, which Swoole can't resolve.
  • Hostnames with only an AAAA record. Swoole opens an IPv4 socket for a hostname, so its lookup fails for such a name. Only then does the adapter ask Swoole's resolver for the IPv6 address and try once more.
    • A name that resolves makes no extra lookup, and a missing name fails as fast as before.
    • Both attempts share one connect timeout.
    • The request is prepared once and both attempts send it, so a body that can be read only once still arrives.

Test Plan

  • bin/monorepo check client passes (Rector, Pint, PHPStan).
  • Client package tests: 257 of 258 pass. The one error is testItRecoversWhenAReusedConnectionIsDropped (Swoole), which is flaky on main too.
  • The destination contract tests still pass on both adapters, so restricted Swoole clients keep refusing disallowed addresses and redirect hops.
  • New tests, each seen failing without its fix:
    • testItConnectsToAnIpv6Literal (both adapters).
    • testItLeavesAHostnameForSwooleToResolve: requests a name that exists only in the resolver Swoole is configured with. Putting the eager getaddrinfo() back fails it with Could not resolve host.
    • testItConnectsToAHostnameWithOnlyAnIpv6Address.
    • testItSendsAOneShotBodyToAHostnameWithOnlyAnIpv6Address: posts a body that yields its bytes once and asserts the server received them.
  • The Swoole resolver tests run against a fake UDP resolver and a loopback listener, each in a separate process because the resolver Swoole asks is a process-wide setting. The IPv6 ones skip when there is no IPv6 loopback.
  • The downstream stack that stalled now boots with this branch pinned.
  • Measured in a Kubernetes pod running Swoole 6.2.2, for a missing service name: getaddrinfo() took 22 ms and 18 ms for the two families, and Swoole's own client failed in 10 ms. Both are fast there, so the stall depends on the cluster's upstream DNS.

🤖 Generated with Claude Code

The Swoole adapter resolved every hostname itself so it could dial an
address its Destinations allow. With Anywhere, the default, there is nothing
to check, so that only changed how every unrestricted client resolves
names: two getaddrinfo() lookups per new connection instead of Swoole's own
resolver. Where a lookup falls through the search domains to a slow
upstream, a request to a missing service waited out the connect timeout
instead of failing at once.

An Anywhere client now passes the hostname to Swoole, as it did before
Destinations existed, and reconnects the same way. Restricted clients still
resolve, check and dial an allowed address.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n loks0n self-assigned this Oct 2, 2026
@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost

ghost commented Oct 2, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

No concrete defects were found in the changed paths or their surrounding contracts.

Unrestricted Swoole clients now leave hostname resolution to Swoole, while restricted clients retain address resolution and destination checks. The change also handles bare IPv6 literals and adds an IPv6-only hostname fallback that reuses the prepared request body and remaining connect-timeout budget. New tests cover native hostname resolution, IPv6 connectivity, and one-shot request bodies.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
packages/client/src/Adapter/SwooleCoroutine/Client.php Delegate unrestricted resolution to Swoole and retry IPv6-only names with the prepared request and remaining timeout.
packages/client/tests/Adapter/AdapterContract.php Add shared coverage for IPv6 literal connectivity and the Host header.
packages/client/tests/Adapter/SwooleCoroutine/ClientTest.php Add isolated fake-DNS tests for native resolution, IPv6-only names, and one-shot body delivery.

Reviewed 35563f5 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-swoole-anywhere-resolution (after).

Metric Before After Change
🚀 Requests/sec 205.68 202.42 ⚪ -1.6%
⏱️ Latency P50 83.68 ms 84.97 ms ⚪ +1.5%
⏱️ Latency P95 196.43 ms 203.23 ms ⚪ +3.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 84.97 203.23 12,597 202.42 +6.8
Account 165.27 311.91 663 10.98 -13.88
TablesDB 81.02 151.2 6,851 111.63 +0.9
Storage 81.33 180.88 3,315 56.01 +17.56
Functions 126.93 260.19 1,768 30.59 +17.92

Top API waits (after)

API request Max wait (ms)
functions.variables.update 512.42
account.name.update 492
functions.create 450.63
account.prefs.update 431.19
functions.variables.create 419.93

A URI's host keeps the brackets of an IPv6 literal, and Swoole fails to
resolve "[::1]". Strip them for the dial address; the Host header keeps
them. A contract test connects both adapters to an IPv6 listener.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Swoole opens an IPv4 socket for a hostname, so leaving resolution to it
fails for a name with only an AAAA record. When the native lookup fails,
and only then, ask Swoole's resolver for the IPv6 address and run the
exchange once more against it. A name that resolves pays nothing, and a
missing name fails as fast as it does natively.

A pooled connection built from an address rather than the hostname is
rebuilt when it has to reconnect, whatever the destinations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

The retry started a fresh connect budget, so a slow failed lookup plus the
IPv6 connect could exceed the configured timeout. It now carries the first
attempt's start time.

The IPv6-only hostname test skips without IPv6 loopback, and asserts the
response rather than the order Swoole's resolver sends its queries in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

The request is prepared once and both attempts send it, so a body that
can only be read once still arrives. The resolver tests share one
fixture, cover the plain IPv4 path, and no longer wait on a fixed delay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@loks0n

loks0n commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@loks0n
loks0n merged commit 35a6757 into main Oct 2, 2026
68 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant