A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi-http: make the p3 outgoing body buffering configurable (#14613)
Xia Chao112e232b382a
diff --git a/crates/wasi-http/src/ctx.rs b/crates/wasi-http/src/ctx.rsindex 48bf044160..a8b9a5ea3d 100644--- a/crates/wasi-http/src/ctx.rs+++ b/crates/wasi-http/src/ctx.rs@@ -390,6 +390,14 @@ pub trait WasiHttpHooks: Send {})}+ /// Number of distinct write calls to the outgoing body's output-stream+ /// that the implementation will buffer.+ /// Default: 1.+ #[cfg(feature = "p3")]+ fn p3_outgoing_body_buffer_chunks(&mut self) -> usize {+ crate::p3::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS+ }+/// Maximum number of bytes the implementation will copy out of the guest in/// a single write to an outgoing body's stream.#[cfg(feature = "p3")]diff --git a/crates/wasi-http/src/p2/body.rs b/crates/wasi-http/src/p2/body.rsindex 8ae7c05741..2f28ac83a5 100644--- a/crates/wasi-http/src/p2/body.rs+++ b/crates/wasi-http/src/p2/body.rs@@ -413,8 +413,11 @@ impl HostOutgoingBody {}}- // always add 1 buffer here because one empty slot is required- let (body_sender, body_receiver) = mpsc::channel(buffer_chunks + 1);+ // always add 1 buffer here because one empty slot is required; the+ // extra slot is part of the clamp since `mpsc::channel` panics above+ // the semaphore's permit limit.+ let capacity = channel_capacity(buffer_chunks.saturating_add(1));+ let (body_sender, body_receiver) = mpsc::channel(capacity);let (finish_sender, finish_receiver) = oneshot::channel();let body_impl = BodyImpl {body_receiver,@@ -513,6 +516,12 @@ impl StreamContext {}}+/// Clamp a requested outgoing body channel capacity into the range that+/// `tokio::sync::mpsc::channel` accepts (it panics outside of it).+fn channel_capacity(requested: usize) -> usize {+ requested.clamp(1, tokio::sync::Semaphore::MAX_PERMITS)+}+/// Provides a [`HostOutputStream`] impl from a [`tokio::sync::mpsc::Sender`].#[derive(Debug)]struct BodyWriteStream {@@ -613,3 +622,25 @@ impl Pollable for BodyWriteStream {let _ = self.writer.reserve().await;}}++#[cfg(test)]+mod tests {+ use super::channel_capacity;++ /// `mpsc::channel` panics above `Semaphore::MAX_PERMITS` and at zero, so the+ /// capacity handed to it must always be clamped - including for the huge+ /// values an embedder or `-Shttp-outgoing-body-buffer-chunks` can supply.+ #[test]+ fn outgoing_body_channel_capacity_is_always_accepted() {+ let max = tokio::sync::Semaphore::MAX_PERMITS;+ for requested in [0, 1, 1024, max, max + 1, usize::MAX] {+ let capacity = channel_capacity(requested);+ assert!(+ capacity >= 1 && capacity <= max,+ "capacity {capacity} out of range"+ );+ // Not panicking is the point: that is what the clamp buys us.+ let _ = tokio::sync::mpsc::channel::<u8>(capacity);+ }+ }+}diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rsindex 1279212a3f..6521a3446f 100644--- a/crates/wasi-http/src/p3/body.rs+++ b/crates/wasi-http/src/p3/body.rs@@ -21,6 +21,12 @@ use wasmtime::component::{use wasmtime::error::Context as _;use wasmtime::{AsContextMut, StoreContextMut};+/// Clamp a requested outgoing body channel capacity into the range that+/// `tokio::sync::mpsc::channel` accepts (it panics outside of it).+fn channel_capacity(requested: usize) -> usize {+ requested.clamp(1, tokio::sync::Semaphore::MAX_PERMITS)+}+/// The concrete type behind a `wasi:http/types.body` resource.pub(crate) enum Body {/// Body constructed by the guest@@ -338,6 +344,10 @@ impl GuestBody {.hooks.p3_outgoing_body_chunk_size().max(1);+ let buffer_chunks = getter(store.as_context_mut().data_mut())+ .hooks+ .p3_outgoing_body_buffer_chunks()+ .max(1);let (trailers_http_tx, trailers_http_rx) = oneshot::channel();trailers_rx.pipe_cb(&mut store, move |data, res| {@@ -357,7 +367,10 @@ impl GuestBody {})?;let contents_rx = if let Some(rx) = contents_rx {- let (http_tx, http_rx) = mpsc::channel(1);+ // `PollSender` can use every slot, so no extra one is needed, but the+ // capacity still has to stay inside what `mpsc::channel` accepts.+ let capacity = channel_capacity(buffer_chunks);+ let (http_tx, http_rx) = mpsc::channel(capacity);let contents_tx = PollSender::new(http_tx);if let Some(limit) = content_length {let (error_tx, error_rx) = oneshot::channel();@@ -738,3 +751,25 @@ pub(crate) trait BodyExt {}impl<T> BodyExt for T {}++#[cfg(test)]+mod tests {+ use super::channel_capacity;++ /// `mpsc::channel` panics above `Semaphore::MAX_PERMITS` and at zero, so the+ /// capacity handed to it must always be clamped - including for the huge+ /// values an embedder or `-Shttp-outgoing-body-buffer-chunks` can supply.+ #[test]+ fn outgoing_body_channel_capacity_is_always_accepted() {+ let max = tokio::sync::Semaphore::MAX_PERMITS;+ for requested in [0, 1, 1024, max, max + 1, usize::MAX] {+ let capacity = channel_capacity(requested);+ assert!(+ capacity >= 1 && capacity <= max,+ "capacity {capacity} out of range"+ );+ // Not panicking is the point: that is what the clamp buys us.+ let _ = tokio::sync::mpsc::channel::<u8>(capacity);+ }+ }+}diff --git a/crates/wasi-http/src/p3/mod.rs b/crates/wasi-http/src/p3/mod.rsindex a809794306..8071d05927 100644--- a/crates/wasi-http/src/p3/mod.rs+++ b/crates/wasi-http/src/p3/mod.rs@@ -20,6 +20,11 @@ mod response;pub use request::Request;pub use response::Response;+/// The default value configured for [`WasiHttpHooks::p3_outgoing_body_buffer_chunks`].+///+/// [`WasiHttpHooks::p3_outgoing_body_buffer_chunks`]: crate::WasiHttpHooks::p3_outgoing_body_buffer_chunks+pub const DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS: usize = 1;+/// The default value configured for [`WasiHttpHooks::p3_outgoing_body_chunk_size`].////// [`WasiHttpHooks::p3_outgoing_body_chunk_size`]: crate::WasiHttpHooks::p3_outgoing_body_chunk_sizediff --git a/src/common.rs b/src/common.rsindex 7433b14db9..0b1ef29196 100644--- a/src/common.rs+++ b/src/common.rs@@ -407,6 +407,20 @@ impl RunCommon {.http_outgoing_body_chunk_size.map(|n| n.get()).unwrap_or(wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_CHUNK_SIZE),+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_buffer_chunks: self+ .common+ .wasi+ .http_outgoing_body_buffer_chunks+ .map(|n| n.get())+ .unwrap_or(wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS),+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_chunk_size: self+ .common+ .wasi+ .http_outgoing_body_chunk_size+ .map(|n| n.get())+ .unwrap_or(wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_CHUNK_SIZE),}}@@ -569,6 +583,10 @@ impl Profile {pub struct HttpHooks {p2_outgoing_body_buffer_chunks: usize,p2_outgoing_body_chunk_size: usize,+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_buffer_chunks: usize,+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_chunk_size: usize,}#[cfg(feature = "wasi-http")]@@ -578,6 +596,11 @@ impl Default for HttpHooks {p2_outgoing_body_buffer_chunks:wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,p2_outgoing_body_chunk_size: wasmtime_wasi_http::p2::DEFAULT_OUTGOING_BODY_CHUNK_SIZE,+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_buffer_chunks:+ wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,+ #[cfg(feature = "component-model-async")]+ p3_outgoing_body_chunk_size: wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_CHUNK_SIZE,}}}@@ -591,4 +614,49 @@ impl wasmtime_wasi_http::WasiHttpHooks for HttpHooks {fn p2_outgoing_body_chunk_size(&mut self) -> usize {self.p2_outgoing_body_chunk_size}++ #[cfg(feature = "component-model-async")]+ fn p3_outgoing_body_buffer_chunks(&mut self) -> usize {+ self.p3_outgoing_body_buffer_chunks+ }++ #[cfg(feature = "component-model-async")]+ fn p3_outgoing_body_chunk_size(&mut self) -> usize {+ self.p3_outgoing_body_chunk_size+ }+}++#[cfg(all(test, feature = "wasi-http", feature = "component-model-async"))]+mod tests {+ use super::RunCommon;+ use clap::Parser as _;+ use std::num::NonZeroUsize;+ use wasmtime_wasi_http::WasiHttpHooks as _;++ /// `-Shttp-outgoing-body-*` lands in `CommonWasiOptions`; from there it has+ /// to reach both the p2 and the p3 hooks.+ #[test]+ fn p3_outgoing_body_options_reach_hooks() {+ let mut cli = RunCommon::try_parse_from(["wasmtime"]).expect("failed to parse options");+ cli.common.wasi.http_outgoing_body_buffer_chunks = NonZeroUsize::new(3);+ cli.common.wasi.http_outgoing_body_chunk_size = NonZeroUsize::new(1024);++ let mut hooks = cli.wasi_http_hooks();+ assert_eq!(hooks.p2_outgoing_body_buffer_chunks(), 3);+ assert_eq!(hooks.p3_outgoing_body_buffer_chunks(), 3);+ assert_eq!(hooks.p2_outgoing_body_chunk_size(), 1024);+ assert_eq!(hooks.p3_outgoing_body_chunk_size(), 1024);++ // Without the options set, both sides keep their documented defaults.+ let cli = RunCommon::try_parse_from(["wasmtime"]).expect("failed to parse options");+ let mut hooks = cli.wasi_http_hooks();+ assert_eq!(+ hooks.p3_outgoing_body_buffer_chunks(),+ wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS,+ );+ assert_eq!(+ hooks.p3_outgoing_body_chunk_size(),+ wasmtime_wasi_http::p3::DEFAULT_OUTGOING_BODY_CHUNK_SIZE,+ );+ }}