Skip to content

Commit 9796cd8

Browse files
authored
add ServiceIndices field to HandlerType::Wasi0_3 (#3449)
This allows embedders to efficiently invoke WASIp3 handlers _without_ using `wasmtime_wasi_http::handler`'s instance reuse machinery if desired. Technically, that could be accomplished without this patch by calling `ProxyHandler::instance_pre` and converting that to a `ServiceIndices` object, but that's expensive to do on each request, so it's better to do it once during setup.
1 parent 2d3a410 commit 9796cd8

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

crates/http/src/trigger.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use wasmtime::component::InstancePre;
55
use wasmtime_wasi::p2::bindings::CommandIndices;
66
use wasmtime_wasi_http::handler::{HandlerState, ProxyHandler, ProxyPre};
77
use wasmtime_wasi_http::p2::bindings::ProxyIndices;
8-
use wasmtime_wasi_http::p3::bindings::ServicePre as P3ProxyPre;
8+
use wasmtime_wasi_http::p3::bindings::{ServiceIndices, ServicePre};
99

1010
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
1111
#[serde(deny_unknown_fields)]
@@ -24,7 +24,7 @@ pub enum HandlerType<S: HandlerState> {
2424
Spin,
2525
Wagi(CommandIndices),
2626
Wasi0_2(ProxyIndices),
27-
Wasi0_3(ProxyHandler<S>),
27+
Wasi0_3(ServiceIndices, ProxyHandler<S>),
2828
Wasi2023_11_10(ProxyIndices2023_11_10),
2929
Wasi2023_10_18(ProxyIndices2023_10_18),
3030
}
@@ -47,11 +47,14 @@ impl<T, S: HandlerState<StoreData = T>> HandlerType<S> {
4747
if let Ok(indices) = ProxyIndices::new(pre) {
4848
candidates.push(HandlerType::Wasi0_2(indices));
4949
}
50-
if let Ok(pre) = P3ProxyPre::new(pre.clone()) {
51-
candidates.push(HandlerType::Wasi0_3(ProxyHandler::new(
52-
handler_state,
53-
ProxyPre::P3(pre),
54-
)));
50+
if let Ok(pre) = ServicePre::new(pre.clone()) {
51+
candidates.push(HandlerType::Wasi0_3(
52+
// We `.unwrap()` here because the `Ok(_)` result from
53+
// `ServicePre::new` above proves that `pre` implements
54+
// `wasi:http/handler@0.3.0-rc-2026-03-15`, so this can't fail.
55+
ServiceIndices::new(pre.instance_pre()).unwrap(),
56+
ProxyHandler::new(handler_state, ProxyPre::P3(pre)),
57+
));
5558
}
5659
if let Ok(indices) = ProxyIndices2023_10_18::new(pre) {
5760
candidates.push(HandlerType::Wasi2023_10_18(indices));

crates/trigger-http/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
404404
.execute(instance_builder, &route_match, req, client_addr)
405405
.await
406406
}
407-
HandlerType::Wasi0_3(handler) => {
407+
HandlerType::Wasi0_3(_, handler) => {
408408
Wasip3HttpExecutor(handler)
409409
.execute(&route_match, req, client_addr)
410410
.await

crates/trigger-http/src/wasi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<S: HandlerState> HttpExecutor for WasiHttpExecutor<'_, S> {
9090
Handler::Handler2023_11_10(guest)
9191
}
9292
HandlerType::Wasi0_2(indices) => Handler::Latest(indices.load(&mut store, &instance)?),
93-
HandlerType::Wasi0_3(_) => unreachable!("should have used Wasip3HttpExecutor"),
93+
HandlerType::Wasi0_3(_, _) => unreachable!("should have used Wasip3HttpExecutor"),
9494
HandlerType::Spin => unreachable!("should have used SpinHttpExecutor"),
9595
HandlerType::Wagi(_) => unreachable!("should have used WagiExecutor instead"),
9696
};

0 commit comments

Comments
 (0)