v0.41.2
This commit is contained in:
1 parent
934d748ea2
commit
8d15429560
13 files changed
+171
-17
No files matched your search
+18
-1
@@ -8460,6 +8460,23 @@
|
||||
"maximum": 86400,
|
||||
"default": 10
|
||||
},
|
||||
"keep_alive_interval": {
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"minimum": 0,
|
||||
"maximum": 3600,
|
||||
"default": 0,
|
||||
"description": "Seconds between QUIC keep-alive PINGs; 0 disables. Must be shorter than max_idle_timeout (unless that is 0). Applies to new QUIC connections."
|
||||
},
|
||||
"keep_alive_mode": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"active_requests",
|
||||
"always"
|
||||
],
|
||||
"default": "active_requests",
|
||||
"description": "active_requests: PINGs keep connections with requests in progress alive; connections without requests are closed after max_idle_timeout. always: every connection is kept alive."
|
||||
},
|
||||
"udp_receive_buffer_bytes": {
|
||||
"type": "integer",
|
||||
"minimum": 0,
|
||||
@@ -16587,4 +16604,4 @@
|
||||
}
|
||||
},
|
||||
"tags": []
|
||||
}
|
||||
}
|
||||
@@ -86,21 +86,22 @@
|
||||
};
|
||||
return { read, sync };
|
||||
}
|
||||
const h3Defaults = { alt_svc_max_age: 3600, max_concurrent_connections: 512, max_concurrent_bidistream: 100, max_concurrent_unistream: 100, max_idle_timeout: 10, udp_receive_buffer_bytes: 4194304, udp_send_buffer_bytes: 4194304 };
|
||||
const h3Defaults = { alt_svc_max_age: 3600, max_concurrent_connections: 512, max_concurrent_bidistream: 100, max_concurrent_unistream: 100, max_idle_timeout: 10, keep_alive_interval: 0, keep_alive_mode: "active_requests", udp_receive_buffer_bytes: 4194304, udp_send_buffer_bytes: 4194304 };
|
||||
function http3SettingsControls(parent, binding) {
|
||||
const box = el("div", undefined, "h3-editor vh-wide"); parent.append(box);
|
||||
const grid = el("div", undefined, "vh-grid"); box.append(grid); const inputs = {};
|
||||
for (const [name, min, max] of [["alt_svc_max_age", 0, 604800], ["max_concurrent_connections", 1, 4096], ["max_concurrent_bidistream", 1, 1024], ["max_concurrent_unistream", 3, 1024], ["max_idle_timeout", 0, 86400]]) inputs[name] = field(grid, name, `h3_${name}`, binding.http3?.[name] ?? h3Defaults[name], "number", { min, max, required: true });
|
||||
for (const [name, min, max] of [["alt_svc_max_age", 0, 604800], ["max_concurrent_connections", 1, 4096], ["max_concurrent_bidistream", 1, 1024], ["max_concurrent_unistream", 3, 1024], ["max_idle_timeout", 0, 86400], ["keep_alive_interval", 0, 3600]]) inputs[name] = field(grid, name, `h3_${name}`, binding.http3?.[name] ?? h3Defaults[name], "number", { min, max, required: true });
|
||||
const keepAliveMode = field(grid, "keep_alive_mode", "h3_keep_alive_mode", binding.http3?.keep_alive_mode ?? h3Defaults.keep_alive_mode, "select", { values: [["active_requests", "h3KeepAliveActiveRequests"], ["always", "h3KeepAliveAlways"]] });
|
||||
for (const direction of ["receive", "send"]) {
|
||||
const name = `udp_${direction}_buffer_bytes`;
|
||||
const mib = (binding.http3?.[name] ?? h3Defaults[name]) / 1048576;
|
||||
inputs[name] = field(grid, name, `h3_udp_${direction}_buffer_mib`, mib, "number", { min: 0, max: 16, step: "any", required: true });
|
||||
}
|
||||
box.append(label("p", "h3TuningHelp", "fine"));
|
||||
const sync = (blocked = false) => { for (const input of Object.values(inputs)) input.disabled = blocked; };
|
||||
const sync = (blocked = false) => { for (const input of Object.values(inputs)) input.disabled = blocked; keepAliveMode.disabled = blocked; };
|
||||
sync(false);
|
||||
const read = () => {
|
||||
const value = {};
|
||||
const value = { keep_alive_mode: keepAliveMode.value };
|
||||
for (const [name, input] of Object.entries(inputs)) {
|
||||
let number = Number(input.value);
|
||||
if (!input.value.trim() || !Number.isFinite(number)) throw fieldError(input, t("vhJsonInvalid"));
|
||||
@@ -111,6 +112,7 @@
|
||||
if (!Number.isSafeInteger(number)) throw fieldError(input, t("vhJsonInvalid"));
|
||||
value[name] = number;
|
||||
}
|
||||
if (value.keep_alive_interval > 0 && value.max_idle_timeout > 0 && value.keep_alive_interval >= value.max_idle_timeout) throw fieldError(inputs.keep_alive_interval, t("h3KeepAliveTooLong"));
|
||||
return value;
|
||||
};
|
||||
return { read, sync };
|
||||
|
||||
+20
-2
@@ -294,6 +294,7 @@ pub async fn serve(bound: Bound, router: Router, state: Arc<App>, grace: Duratio
|
||||
};
|
||||
let router=router.clone();let state=state.clone();let address=bound.address;
|
||||
let limit=settings.max_concurrent_bidistream as usize;
|
||||
let idle_close=settings.idle_close_without_requests();
|
||||
tasks.spawn(async move {
|
||||
let connection=tokio::select! {
|
||||
_=state.shutdown.cancelled()=>return,
|
||||
@@ -328,7 +329,7 @@ pub async fn serve(bound: Bound, router: Router, state: Arc<App>, grace: Duratio
|
||||
state.metrics.http3_active_connections.fetch_add(1,Ordering::Relaxed);
|
||||
state.metrics.tls_connections.fetch_add(1,Ordering::Relaxed);
|
||||
let _lease=Lease {_permit:permit,metrics:state.metrics.clone()};
|
||||
if let Err(error)=connection_loop(connection,local,router,state.clone(),limit,grace,tls_sni.clone()).await {
|
||||
if let Err(error)=connection_loop(connection,local,router,state.clone(),limit,grace,tls_sni.clone(),idle_close).await {
|
||||
log_protocol_error(
|
||||
&state, "http3_connection_error", &error, peer, local, 0, None, None, tls_sni,
|
||||
);
|
||||
@@ -353,6 +354,7 @@ async fn connection_loop(
|
||||
limit: usize,
|
||||
grace: Duration,
|
||||
tls_sni: Option<String>,
|
||||
idle_close: Option<Duration>,
|
||||
) -> Result<()> {
|
||||
let mut builder = h3::server::builder();
|
||||
builder.max_field_section_size(32 * 1024);
|
||||
@@ -364,6 +366,10 @@ async fn connection_loop(
|
||||
let mut draining = false;
|
||||
let deadline = tokio::time::sleep(Duration::from_secs(86400));
|
||||
tokio::pin!(deadline);
|
||||
let idle_after = idle_close.unwrap_or(Duration::from_secs(86400));
|
||||
let idle = tokio::time::sleep(idle_after);
|
||||
tokio::pin!(idle);
|
||||
let mut idle_closed = false;
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
@@ -372,7 +378,15 @@ async fn connection_loop(
|
||||
h3.shutdown(0).await?;
|
||||
},
|
||||
_=&mut deadline,if draining=>break,
|
||||
_=requests.join_next(),if !requests.is_empty()=>{},
|
||||
_=requests.join_next(),if !requests.is_empty()=>{
|
||||
if requests.is_empty() { idle.as_mut().reset(tokio::time::Instant::now()+idle_after); }
|
||||
},
|
||||
_=&mut idle,if idle_close.is_some() && requests.is_empty() && !draining=>{
|
||||
tracing::debug!(peer=%connection.remote_address(), "HTTP/3 connection idle without requests; closing");
|
||||
draining=true;idle_closed=true;
|
||||
deadline.as_mut().reset(tokio::time::Instant::now()+grace.min(Duration::from_secs(1)));
|
||||
h3.shutdown(0).await?;
|
||||
},
|
||||
next=h3.accept()=>match next {
|
||||
Ok(Some(resolver))=>{
|
||||
if requests.len()>=limit {
|
||||
@@ -556,6 +570,10 @@ async fn connection_loop(
|
||||
.await;
|
||||
requests.abort_all();
|
||||
while requests.join_next().await.is_some() {}
|
||||
if idle_closed {
|
||||
connection.close(0x100u32.into(), b"idle");
|
||||
return Ok(());
|
||||
}
|
||||
if draining {
|
||||
let _ = tokio::time::timeout(grace, connection.closed()).await;
|
||||
}
|
||||
|
||||
+33
-1
@@ -1,3 +1,11 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum KeepAliveMode {
|
||||
#[default]
|
||||
ActiveRequests,
|
||||
Always,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct Settings {
|
||||
@@ -6,6 +14,8 @@ pub struct Settings {
|
||||
pub max_concurrent_bidistream: u32,
|
||||
pub max_concurrent_unistream: u32,
|
||||
pub max_idle_timeout: u64,
|
||||
pub keep_alive_interval: u64,
|
||||
pub keep_alive_mode: KeepAliveMode,
|
||||
pub udp_receive_buffer_bytes: u32,
|
||||
pub udp_send_buffer_bytes: u32,
|
||||
}
|
||||
@@ -17,6 +27,8 @@ impl Default for Settings {
|
||||
max_concurrent_bidistream: 100,
|
||||
max_concurrent_unistream: 100,
|
||||
max_idle_timeout: 10,
|
||||
keep_alive_interval: 0,
|
||||
keep_alive_mode: KeepAliveMode::ActiveRequests,
|
||||
udp_receive_buffer_bytes: 4 * 1024 * 1024,
|
||||
udp_send_buffer_bytes: 4 * 1024 * 1024,
|
||||
}
|
||||
@@ -41,6 +53,16 @@ impl Settings {
|
||||
self.max_idle_timeout <= 86400,
|
||||
"HTTP/3 idle timeout must be 0..86400 seconds"
|
||||
);
|
||||
ensure!(
|
||||
self.keep_alive_interval <= 3600,
|
||||
"HTTP/3 keep-alive interval must be 0..3600 seconds"
|
||||
);
|
||||
ensure!(
|
||||
self.keep_alive_interval == 0
|
||||
|| self.max_idle_timeout == 0
|
||||
|| self.keep_alive_interval < self.max_idle_timeout,
|
||||
"HTTP/3 keep-alive interval must be shorter than the QUIC idle timeout"
|
||||
);
|
||||
for (name, value) in [
|
||||
("receive", self.udp_receive_buffer_bytes),
|
||||
("send", self.udp_send_buffer_bytes),
|
||||
@@ -66,7 +88,17 @@ impl Settings {
|
||||
.stream_receive_window((256 * 1024u32).into())
|
||||
.receive_window((8 * 1024 * 1024u32).into())
|
||||
.send_window(8 * 1024 * 1024)
|
||||
.datagram_receive_buffer_size(None);
|
||||
.datagram_receive_buffer_size(None)
|
||||
.keep_alive_interval(
|
||||
(self.keep_alive_interval > 0)
|
||||
.then(|| Duration::from_secs(self.keep_alive_interval)),
|
||||
);
|
||||
Ok(transport)
|
||||
}
|
||||
pub fn idle_close_without_requests(&self) -> Option<Duration> {
|
||||
(self.keep_alive_interval > 0
|
||||
&& self.keep_alive_mode == KeepAliveMode::ActiveRequests
|
||||
&& self.max_idle_timeout > 0)
|
||||
.then(|| Duration::from_secs(self.max_idle_timeout))
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,54 @@ mod tests {
|
||||
.is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn keep_alive_is_off_by_default_and_validated() {
|
||||
let s = Settings::default();
|
||||
assert_eq!(s.keep_alive_interval, 0);
|
||||
assert_eq!(s.keep_alive_mode, KeepAliveMode::ActiveRequests);
|
||||
assert!(s.idle_close_without_requests().is_none());
|
||||
let on = Settings {
|
||||
max_idle_timeout: 30,
|
||||
keep_alive_interval: 15,
|
||||
..s
|
||||
};
|
||||
assert!(on.transport().is_ok());
|
||||
assert_eq!(
|
||||
on.idle_close_without_requests(),
|
||||
Some(Duration::from_secs(30))
|
||||
);
|
||||
assert!(Settings {
|
||||
keep_alive_mode: KeepAliveMode::Always,
|
||||
..on
|
||||
}
|
||||
.idle_close_without_requests()
|
||||
.is_none());
|
||||
assert!(Settings {
|
||||
max_idle_timeout: 10,
|
||||
keep_alive_interval: 10,
|
||||
..s
|
||||
}
|
||||
.validate()
|
||||
.is_err());
|
||||
let unlimited = Settings {
|
||||
max_idle_timeout: 0,
|
||||
keep_alive_interval: 20,
|
||||
..s
|
||||
};
|
||||
assert!(unlimited.validate().is_ok());
|
||||
assert!(unlimited.idle_close_without_requests().is_none());
|
||||
assert!(Settings {
|
||||
keep_alive_interval: 3601,
|
||||
max_idle_timeout: 0,
|
||||
..s
|
||||
}
|
||||
.validate()
|
||||
.is_err());
|
||||
let json: Settings =
|
||||
serde_json::from_str(r#"{"keep_alive_interval":5,"keep_alive_mode":"always"}"#)
|
||||
.unwrap();
|
||||
assert_eq!(json.keep_alive_mode, KeepAliveMode::Always);
|
||||
}
|
||||
#[test]
|
||||
fn critical_streams_and_resource_limits_are_validated() {
|
||||
assert!(Settings {
|
||||
max_concurrent_unistream: 2,
|
||||
|
||||
Reference in new issue
Block a user