A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Document that `ProxyHandler::handle` does not handle backpressure (#13555)
Alex Crichton9eb4889d5077
diff --git a/crates/wasi-http/src/handler.rs b/crates/wasi-http/src/handler.rsindex 536ef772ac..b620c2716a 100644--- a/crates/wasi-http/src/handler.rs+++ b/crates/wasi-http/src/handler.rs@@ -1109,6 +1109,20 @@ where/// In other failure cases (e.g. `wasi:http/types#error-code` return values/// and/or traps when executing synchronous WASIp2 handler functions), the/// original error returned by the handler will be returned.+ ///+ /// # Backpressure+ ///+ /// Note that this API does not implement any form of backpressure to limit+ /// the number of in-flight `Request`s being processed. This function+ /// may spawn new tokio tasks, instantiate new modules under new stores, and+ /// queue up pending `Request`s while waiting for previous instances. In all+ /// of these situations invoking this function will consume some host-side+ /// resources until the request is done.+ ///+ /// Embedders using this API must ensure to take this into account. If an+ /// infinite number of requests can be fed into this function then it's+ /// recommended to take a semaphore, for example, around this function call+ /// to limit the number of concurrent requests that are being processed.pub async fn handle(&self,id: <S::WorkerState as WorkerState>::RequestId,diff --git a/src/commands/serve.rs b/src/commands/serve.rsindex 6bd6f974fc..b55feb3fab 100644--- a/src/commands/serve.rs+++ b/src/commands/serve.rs@@ -19,7 +19,7 @@ use std::{time::{Duration, Instant},};use tokio::io::{self, AsyncWrite};-use tokio::sync::Notify;+use tokio::sync::{Notify, Semaphore};use wasmtime::component::{Component, GuestTaskId, Linker};use wasmtime::error::Context as _;use wasmtime::{@@ -169,6 +169,15 @@ pub struct ServeCommand {/// one header per line.#[arg(short = 'H', long = "header", value_name = "HEADER")]headers: Vec<String>,++ /// Maximum number of concurrent requests that can be processed at any one+ /// point in time.+ #[arg(long)]+ max_concurrent_requests: Option<usize>,+ /// Maximum number of concurrent connections that can be held at any one+ /// point in time.+ #[arg(long)]+ max_concurrent_connections: Option<usize>,}impl ServeCommand {@@ -679,8 +688,12 @@ impl ServeCommand {} else {1};+ let sem_connections = Arc::new(Semaphore::new(+ self.max_concurrent_connections.unwrap_or(1000),+ ));let handler = ProxyHandler::new(HostHandlerState {+ sem_requests: Semaphore::new(self.max_concurrent_requests.unwrap_or(1000)),cmd: self,engine,component,@@ -699,9 +712,13 @@ impl ServeCommand {// Wait for a socket, but also "race" against shutdown to break out// of this loop. Once the graceful shutdown signal is received then// this loop exits immediately.- let (stream, _) = tokio::select! {+ let (connection_permit, stream) = tokio::select! {_ = shutdown.requested.notified() => break,- v = listener.accept() => v?,+ v = async {+ let permit = sem_connections.clone().acquire_owned().await?;+ let (stream, _) = listener.accept().await?;+ wasmtime::error::Ok((permit, stream))+ } => v?,};// The Nagle algorithm can impose a significant latency penalty@@ -764,9 +781,13 @@ impl ServeCommand {eprintln!("error: {e:?}");}drop(shutdown_guard);+ drop(connection_permit);});}+ // Don't allow any further requests to get picked up.+ handler.state().sem_requests.close();+drop(handler);// Upon exiting the loop we'll no longer process any more incoming@@ -883,6 +904,7 @@ struct HostHandlerState {instance: ProxyPre<Host>,next_instance_id: AtomicU64,next_request_id: AtomicU64,+ sem_requests: Semaphore,_shutdown_guard: Box<dyn std::any::Any + Send + Sync>,}@@ -1256,6 +1278,11 @@ async fn handle_request() -> Result<hyper::Response<UnsyncBoxBody<Bytes, wasmtime::Error>>> {use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;+ // This is used to throttle the maximum number of concurrent requests that+ // can be processed at any one point in time before delegating to+ // `handler.handle(...)` below.+ let _request_permit = handler.state().sem_requests.acquire().await?;+handler.state().request_headers.apply(req.headers_mut());let request_id = handler