Skip to content

feat: add a warm delay option to pingBatch - #174

Merged
yiguodev merged 3 commits into
XTLS:mainfrom
LaunchVPN:feat/ping-batch-warm
Nov 22, 2026
Merged

yiguodev merged 3 commits into
XTLS:mainfrom
LaunchVPN:feat/ping-batch-warm

Conversation

@ForeverInLaw

@ForeverInLaw ForeverInLaw commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

pingBatch payload { configs, timeout, url, warm }
└─ per item, one deadline
   ├─ PingBatch / warm=false (default): unchanged, HEAD on a new connection → delay
   └─ PingBatchWarm / warm=true:        HEAD, HEAD on the same connection → delay of the second
                                        through Transport.RoundTrip, a redirect is the answer
                                        the second request never dials; not kept alive → 10000

A single HEAD on a new connection also times the TCP connect, the proxy handshake and the TLS handshake to the target, so the delay comes out several times the round trip through the outbound. With warm, the second request reuses the connection and leaves those handshakes out, the way mihomo's unified delay works. Both requests share the configured timeout, and an item fails if either does.

A warm probe sends both requests through Transport.RoundTrip, so a redirect response is the answer, even with a malformed Location, and each request stays one round trip. The second request never opens a new connection: when the first one was not kept alive, for example after Connection: close, the item fails with 10000 instead of timing a fresh handshake. Credentials in the URL are sent as Basic auth, as http.Client does for the default probe.

PingBatch keeps its signature and its request: one HEAD with keep-alive disabled, timed by nodep.PingHTTPRequest. Go callers get the warm probe from PingBatchWarm, and Invoke picks it when warm is set. The 10000/11000 values, the result shape, the five-item limit, input order and per-item failure isolation stay as they are. The English and Chinese READMEs describe the flag.

Evidence

  • Before: one client network, medians of five runs, in ms: server A 141 (ICMP 56), server B 222 (TCP connect 44), a relayed server 446–865.
    After: with warm, the same servers report 69, 55 and 141–174.
  • TestPingBatchWarmTimesSecondRequestOnOneConnection: a slow first answer stays out of the delay, and both requests arrive on one connection.
  • TestPingBatchWarmFailsWithoutKeepAlive: a server that sends Connection: close and stalls any further request gets 10000 and sees only one request.
  • TestPingBatchRedirects: a same-origin 302 takes two connections by default, as on main, and the warm probe answers with the redirect on one connection, also when Location is malformed.
  • TestPingBatchSendsURLCredentials: both probes send user:pass@ from the URL with every request.
  • TestPingBatchTimesOutWithinOneTimeout: by default a stalled HEAD reports 11000. With warm, the first HEAD answers at 1.2 s and the second needs another 1.2 s of a 2 s timeout; it reports 11000 within that timeout after the server saw both requests on one connection. A fresh deadline for the second request makes this test fail.
  • TestInvokePingBatchWarmTimesSecondRequest: warm reaches the batch through Invoke.
  • Passed go test ./... -count=1, go vet, gofmt and git diff --check.

A single HEAD on a new connection also times the TCP, proxy and TLS
handshakes, so the delay comes out several times the round trip through
the outbound. With warm set, each item sends two HEAD requests on one
connection and reports the second; both share the configured timeout.
The default stays one request on a new connection.

@yiguodev yiguodev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I tested 4ad4a87 with go test ./... -count=1 and local HTTP servers. The suite passed, and both warm requests share the configured deadline. I found four things to address before merging.

  1. Warm probes can report a new connection as a success. When the first response sends Connection: close, the transport opens a second TCP connection and the probe still returns success: true. The reported delay can include a fresh handshake. Please ensure both requests use the same connection and define how warm probes handle redirects or unavailable keep-alive. Request loop
  2. The default probe changes how it follows same-origin redirects. A local 302 test used two connections on main and one on this PR with warm=false. Setting DisableKeepAlives: !warm would preserve the existing default measurement. Transport setup
  3. The exported Go signature breaks existing callers. Three-argument calls to xray.PingBatch no longer compile. The JSON Invoke field remains optional. Please keep the existing Go signature and expose warm probing through a separate entry point. Function signature
  4. The timeout test only stalls the first HEAD. It would pass even if the second HEAD received a fresh deadline. Please let the first request finish, then have the second exceed the remaining budget and assert the 11000 timeout result. Timeout test

PingBatch keeps its signature and its request; PingBatchWarm runs the
warm probe, and Invoke picks it when warm is set. A warm probe does not
follow redirects, and the item fails when the second request cannot
reuse the connection. The timeout test now lets the first request
answer and has the second exceed the remaining budget.
@ForeverInLaw

Copy link
Copy Markdown
Contributor Author

Thanks for testing it. All four are fixed in 8c9efea, on top of the commit you reviewed.

  1. A warm probe now fails the item with 10000 when the second request does not reuse the first connection. It does not follow redirects, so a redirect response is the answer and each request stays one round trip. The READMEs state both rules. Covered by TestPingBatchWarmFailsWithoutKeepAlive and TestPingBatchRedirects.
  2. The default probe is the code from main again: keep-alive disabled, timed by nodep.PingHTTPRequest. TestPingBatchRedirects checks that a same-origin 302 still takes two connections.
  3. PingBatch keeps its three-argument signature. The warm probe is PingBatchWarm, and Invoke picks it when warm is set.
  4. In the warm timeout test the first HEAD now answers at 1.2 s, and the second needs another 1.2 s of a 2 s timeout. It reports 11000 after the server saw both requests, and with a fresh deadline for the second request the test fails.

@yiguodev

Copy link
Copy Markdown
Collaborator

Thanks for the update. I retested 8c9efea. The three-argument PingBatch API and the default probe behavior are restored, and the revised timeout test now exercises the second HEAD. I reproduced two remaining issues in the warm path:

  1. The second HEAD can still go out on a fresh connection. The reuse check runs after the response and only on success. With Connection: close on the first response and a stalled response on the replacement connection, the server received two requests on two connections and the probe returned 11000. Please prevent a new dial during the second HEAD so unavailable reuse returns 10000. A timeout on the reused connection should still return 11000. The keep-alive regression test can also assert that the server only receives one request. Reuse check
  2. A malformed redirect location still fails the warm probe. http.Client parses Location before CheckRedirect runs. I reproduced 10000 on the first HEAD with a 302 and Location: /%. A warm probe can use Transport.RoundTrip directly so it treats the 302 as the response without processing the redirect. Redirect handling

I recommend fixing both before merging.

The second HEAD now fails with 10000 instead of opening a new
connection, so a stall on a replacement connection can no longer turn
into 11000, and the server sees only one request. Both requests go
through Transport.RoundTrip, so a redirect with a malformed Location is
still the answer; credentials in the URL are still sent, as
http.Client sends them for the default probe.
@ForeverInLaw

Copy link
Copy Markdown
Contributor Author

Thanks, both reproduced and fixed in 64ae49c.

  1. In warm mode the transport now dials only once. A second HEAD that would need a new connection fails at once with 10000 and never reaches the server; a timeout on the reused connection still returns 11000. TestPingBatchWarmFailsWithoutKeepAlive sends Connection: close, stalls any further request, and asserts 10000 and a single request. The warm timeout test also asserts one connection.
  2. Both warm requests go through Transport.RoundTrip, so a 302 is the response without processing Location. TestPingBatchRedirects adds a Location: /% case. Since RoundTrip skips what http.Client does with user:pass@ in the URL, the warm probe sets that Basic auth itself, and TestPingBatchSendsURLCredentials checks both modes.

@yiguodev
yiguodev merged commit 6e33510 into XTLS:main Nov 22, 2026
2 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.

2 participants