Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 84d3100

Browse files
jasnelladuh95
authored andcommitted
quic: add initial RTT option to session options
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: OpenCode:Opus 4.6 PR-URL: #63267 Backport-PR-URL: #64675 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent a2b6a81 commit 84d3100

6 files changed

Lines changed: 109 additions & 4 deletions

File tree

β€Ždoc/api/quic.mdβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,23 @@ added: v23.8.0
27232723
Specifies the maximum number of milliseconds a TLS handshake is permitted to take
27242724
to complete before timing out.
27252725

2726+
#### `sessionOptions.initialRtt`
2727+
2728+
<!-- YAML
2729+
added: REPLACEME
2730+
-->
2731+
2732+
* Type: {bigint|number}
2733+
***Default:**`0` (use ngtcp2 default of 333ms)
2734+
2735+
Specifies the initial round-trip time estimate in milliseconds. This value is
2736+
used for probe timeout (PTO) computation, initial pacing, and early loss
2737+
detection before the first actual RTT sample is collected from the connection.
2738+
The default of 333ms is appropriate for the general internet. For low-latency
2739+
environments such as loopback or same-rack deployments, setting a value closer
2740+
to the actual RTT (e.g., `1`) avoids unnecessarily conservative initial
2741+
behavior.
2742+
27262743
#### `sessionOptions.keepAlive`
27272744

27282745
<!-- YAML

β€Žlib/internal/quic/quic.jsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ const endpointRegistry = new SafeSet();
404404
* @property {ArrayBufferView} [token] An opaque address validation token
405405
* previously received from the server via `onnewtoken` (client only).
406406
* @property {bigint|number} [handshakeTimeout] The handshake timeout
407+
* @property {bigint|number} [initialRtt] The initial round-trip time estimate in milliseconds.
408+
* Used for PTO computation and initial pacing before the first RTT sample. Default uses
409+
* ngtcp2's built-in default of 333ms. Set lower for low-latency environments.
407410
* @property {bigint|number} [keepAlive] The keep-alive timeout in milliseconds. When set,
408411
* PING frames will be sent automatically to prevent idle timeout.
409412
* @property {bigint|number} [maxStreamWindow] The maximum stream window
@@ -4875,6 +4878,7 @@ function processSessionOptions(options, config = kEmptyObject) {
48754878
maxPayloadSize,
48764879
unacknowledgedPacketThreshold =0,
48774880
handshakeTimeout,
4881+
initialRtt,
48784882
keepAlive,
48794883
maxStreamWindow,
48804884
maxWindow,
@@ -4982,6 +4986,7 @@ function processSessionOptions(options, config = kEmptyObject) {
49824986
maxPayloadSize,
49834987
unacknowledgedPacketThreshold,
49844988
handshakeTimeout,
4989+
initialRtt,
49854990
keepAlive,
49864991
maxStreamWindow,
49874992
maxWindow,

β€Žsrc/quic/bindingdata.hβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class SessionManager;
9393
V(groups, "groups") \
9494
V(handshake_timeout, "handshakeTimeout") \
9595
V(http3_alpn, &NGHTTP3_ALPN_H3[1]) \
96+
V(initial_rtt, "initialRtt") \
9697
V(keep_alive_timeout, "keepAlive") \
9798
V(initial_max_data, "initialMaxData") \
9899
V(initial_max_stream_data_bidi_local, "initialMaxStreamDataBidiLocal") \

β€Žsrc/quic/session.ccβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ Session::Config::Config(Environment* env,
513513
options.handshake_timeout == UINT64_MAX
514514
? UINT64_MAX
515515
: options.handshake_timeout * NGTCP2_MILLISECONDS;
516+
517+
// The initial_rtt option is in milliseconds; ngtcp2 expects nanoseconds.
518+
// A value of 0 leaves the ngtcp2 default (333ms) unchanged.
519+
if (options.initial_rtt > 0)
520+
settings.initial_rtt = options.initial_rtt * NGTCP2_MILLISECONDS;
521+
516522
settings.max_stream_window = options.max_stream_window;
517523
settings.max_window = options.max_window;
518524
settings.ack_thresh = options.unacknowledged_packet_threshold;
@@ -604,10 +610,11 @@ Maybe<Session::Options> Session::Options::From(Environment* env,
604610

605611
if (!SET(version) || !SET(min_version) || !SET(preferred_address_strategy) ||
606612
!SET(transport_params) || !SET(tls_options) || !SET(qlog) ||
607-
!SET(handshake_timeout) || !SET(keep_alive_timeout) ||
608-
!SET(max_stream_window) || !SET(max_window) || !SET(max_payload_size) ||
609-
!SET(unacknowledged_packet_threshold) || !SET(cc_algorithm) ||
610-
!SET(draining_period_multiplier) || !SET(max_datagram_send_attempts)) {
613+
!SET(handshake_timeout) || !SET(initial_rtt) ||
614+
!SET(keep_alive_timeout) || !SET(max_stream_window) || !SET(max_window) ||
615+
!SET(max_payload_size) || !SET(unacknowledged_packet_threshold) ||
616+
!SET(cc_algorithm) || !SET(draining_period_multiplier) ||
617+
!SET(max_datagram_send_attempts)) {
611618
return Nothing<Options>();
612619
}
613620

@@ -726,6 +733,12 @@ std::string Session::Options::ToString() const {
726733
res += prefix + "handshake timeout: " + std::to_string(handshake_timeout) +
727734
" nanoseconds";
728735
}
736+
if (initial_rtt > 0) {
737+
res += prefix + "initial rtt: " + std::to_string(initial_rtt) +
738+
" milliseconds";
739+
} else {
740+
res += prefix + "initial rtt: <default>";
741+
}
729742
res += prefix + "max stream window: " + std::to_string(max_stream_window);
730743
res += prefix + "max window: " + std::to_string(max_window);
731744
res += prefix + "max payload size: " + std::to_string(max_payload_size);

β€Žsrc/quic/session.hβ€Ž

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ class Session final : public AsyncWrap, private SessionTicket::AppData::Source {
163163
staticconstexpruint64_tDEFAULT_HANDSHAKE_TIMEOUT = 10'000;
164164
uint64_t handshake_timeout = DEFAULT_HANDSHAKE_TIMEOUT;
165165

166+
// The initial round-trip time estimate in milliseconds. ngtcp2 uses this
167+
// for PTO computation, initial pacing, and early loss detection before
168+
// the first RTT sample is collected. The default of 0 uses ngtcp2's
169+
// built-in default of 333ms, which is appropriate for the general
170+
// internet. For low-latency environments (e.g., loopback or same-rack
171+
// deployments), setting a value closer to the actual RTT avoids
172+
// unnecessarily conservative initial behavior.
173+
uint64_t initial_rtt = 0;
174+
166175
// The keep-alive timeout in milliseconds. When set to a non-zero value,
167176
// ngtcp2 will automatically send PING frames to keep the connection alive
168177
// before the idle timeout fires. Set to 0 to disable (default).
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: initialRtt session option is accepted and the session functions
4+
// correctly with a custom initial RTT estimate.
5+
6+
import{hasQuic,skip,mustCall}from'../common/index.mjs';
7+
importassertfrom'node:assert';
8+
9+
const{ ok }=assert;
10+
11+
if(!hasQuic){
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const{ listen, connect }=awaitimport('../common/quic.mjs');
16+
const{ bytes }=awaitimport('stream/iter');
17+
18+
constencoder=newTextEncoder();
19+
constpayload=encoder.encode('hello rtt');
20+
constserverDone=Promise.withResolvers();
21+
22+
// Use a low initialRtt (1ms) to simulate a low-latency environment.
23+
// The session should complete successfully and the smoothed RTT in
24+
// stats should converge to a value well below the default 333ms.
25+
constserverEndpoint=awaitlisten(mustCall((serverSession)=>{
26+
serverSession.onstream=mustCall(async(stream)=>{
27+
constdata=awaitbytes(stream);
28+
ok(data.byteLength>0);
29+
stream.writer.endSync();
30+
awaitstream.closed;
31+
serverSession.close();
32+
serverDone.resolve();
33+
});
34+
}),{
35+
initialRtt: 1,// 1ms
36+
});
37+
38+
constclientSession=awaitconnect(serverEndpoint.address,{
39+
initialRtt: 1,// 1ms
40+
});
41+
awaitclientSession.opened;
42+
43+
conststream=awaitclientSession.createBidirectionalStream({
44+
body: payload,
45+
});
46+
47+
forawait(const_ofstream){/* drain */}// eslint-disable-line no-unused-vars
48+
awaitstream.closed;
49+
awaitserverDone.promise;
50+
51+
// After data exchange, the smoothed RTT should have converged to a
52+
// realistic value. On loopback it should be well under 10ms (10,000,000ns).
53+
// The stat is in nanoseconds.
54+
constsmoothedRtt=clientSession.stats.smoothedRtt;
55+
ok(smoothedRtt>0n,'smoothedRtt should be non-zero after data exchange');
56+
ok(smoothedRtt<10_000_000n,
57+
`smoothedRtt should be under 10ms on loopback, got ${smoothedRtt}ns`);
58+
59+
awaitclientSession.close();
60+
awaitserverEndpoint.close();

0 commit comments

Comments
Β (0)