Skip to content

Commit 30962b7

Browse files
committed
Working generic factors
Signed-off-by: Ryan Levick <[email protected]>
1 parent 855572b commit 30962b7

File tree

16 files changed

+213
-138
lines changed

16 files changed

+213
-138
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ spin-build = { path = "crates/build" }
4848
spin-common = { path = "crates/common" }
4949
spin-doctor = { path = "crates/doctor" }
5050
spin-expressions = { path = "crates/expressions" }
51+
spin-factors = { path = "crates/factors" }
5152
spin-http = { path = "crates/http" }
5253
spin-loader = { path = "crates/loader" }
5354
spin-locked-app = { path = "crates/locked-app" }
5455
spin-manifest = { path = "crates/manifest" }
5556
spin-oci = { path = "crates/oci" }
5657
spin-plugins = { path = "crates/plugins" }
58+
spin-runtime-config = { path = "crates/runtime-config" }
5759
spin-telemetry = { path = "crates/telemetry", features = [
5860
"tracing-log-compat",
5961
] }

crates/factors/src/runtime_factors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use crate::{factor::FactorInstanceState, App, ConfiguredApp, Factor};
3030
/// // Instantiate the component
3131
/// let instance = linker.instantiate_async(&mut store, &component).await?;
3232
/// ```
33-
pub trait RuntimeFactors: Sized + 'static {
33+
pub trait RuntimeFactors: Send + Sync + Sized + 'static {
3434
/// The per application state of all the factors.
35-
type AppState: Sync;
35+
type AppState: Sync + Send;
3636
/// The per instance state of the factors.
3737
type InstanceState: RuntimeFactorsInstanceState;
3838
/// The collection of all the `InstanceBuilder`s of the factors.
39-
type InstanceBuilders;
39+
type InstanceBuilders: Send;
4040
/// The runtime configuration of all the factors.
4141
type RuntimeConfig: Default;
4242

crates/trigger-http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ serde = { version = "1.0", features = ["derive"] }
3131
serde_json = "1"
3232
spin-app = { path = "../app" }
3333
spin-core = { path = "../core" }
34+
spin-factors = { path = "../factors" }
3435
spin-factor-outbound-http = { path = "../factor-outbound-http" }
3536
spin-factor-wasi = { path = "../factor-wasi" }
3637
spin-http = { path = "../http" }

crates/trigger-http/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use anyhow::{bail, Context};
2020
use clap::Args;
2121
use serde::Deserialize;
2222
use spin_app::App;
23+
use spin_factors::RuntimeFactors;
2324
use spin_trigger::Trigger;
2425
use wasmtime_wasi_http::bindings::wasi::http::types::ErrorCode;
2526

@@ -29,8 +30,9 @@ pub use tls::TlsConfig;
2930

3031
pub(crate) use wasmtime_wasi_http::body::HyperIncomingBody as Body;
3132

32-
pub(crate) type TriggerApp = spin_trigger::TriggerApp<HttpTrigger>;
33-
pub(crate) type TriggerInstanceBuilder<'a> = spin_trigger::TriggerInstanceBuilder<'a, HttpTrigger>;
33+
pub(crate) type TriggerApp<F> = spin_trigger::TriggerApp<HttpTrigger, F>;
34+
pub(crate) type TriggerInstanceBuilder<'a, F> =
35+
spin_trigger::TriggerInstanceBuilder<'a, HttpTrigger, F>;
3436

3537
#[derive(Args)]
3638
pub struct CliArgs {
@@ -70,7 +72,7 @@ pub struct HttpTrigger {
7072
tls_config: Option<TlsConfig>,
7173
}
7274

73-
impl Trigger for HttpTrigger {
75+
impl<F: RuntimeFactors> Trigger<F> for HttpTrigger {
7476
const TYPE: &'static str = "http";
7577

7678
type CliArgs = CliArgs;
@@ -80,7 +82,7 @@ impl Trigger for HttpTrigger {
8082
Self::new(app, cli_args.address, cli_args.into_tls_config())
8183
}
8284

83-
async fn run(self, trigger_app: TriggerApp) -> anyhow::Result<()> {
85+
async fn run(self, trigger_app: TriggerApp<F>) -> anyhow::Result<()> {
8486
let server = self.into_server(trigger_app)?;
8587

8688
server.serve().await?;
@@ -109,7 +111,10 @@ impl HttpTrigger {
109111
}
110112

111113
/// Turn this [`HttpTrigger`] into an [`HttpServer`].
112-
pub fn into_server(self, trigger_app: TriggerApp) -> anyhow::Result<Arc<HttpServer>> {
114+
pub fn into_server<F: RuntimeFactors>(
115+
self,
116+
trigger_app: TriggerApp<F>,
117+
) -> anyhow::Result<Arc<HttpServer<F>>> {
113118
let Self {
114119
listen_addr,
115120
tls_config,

crates/trigger-http/src/outbound_http.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ use spin_factor_outbound_http::{
88
HostFutureIncomingResponse, InterceptOutcome, OutgoingRequestConfig, Request,
99
};
1010
use spin_factor_outbound_networking::parse_service_chaining_target;
11+
use spin_factors::RuntimeFactors;
1112
use spin_http::routes::RouteMatch;
1213
use wasmtime_wasi_http::types::IncomingResponse;
1314

1415
use crate::HttpServer;
1516

1617
/// An outbound HTTP interceptor that handles service chaining requests.
17-
pub struct OutboundHttpInterceptor {
18-
server: Arc<HttpServer>,
18+
pub struct OutboundHttpInterceptor<F: RuntimeFactors> {
19+
server: Arc<HttpServer<F>>,
1920
}
2021

21-
impl OutboundHttpInterceptor {
22-
pub fn new(server: Arc<HttpServer>) -> Self {
22+
impl<F: RuntimeFactors> OutboundHttpInterceptor<F> {
23+
pub fn new(server: Arc<HttpServer<F>>) -> Self {
2324
Self { server }
2425
}
2526
}
2627

2728
const CHAINED_CLIENT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
2829

29-
impl spin_factor_outbound_http::OutboundHttpInterceptor for OutboundHttpInterceptor {
30+
impl<F: RuntimeFactors> spin_factor_outbound_http::OutboundHttpInterceptor
31+
for OutboundHttpInterceptor<F>
32+
{
3033
fn intercept(
3134
&self,
3235
request: &mut Request,

crates/trigger-http/src/server.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use hyper::{
1414
use hyper_util::rt::TokioIo;
1515
use spin_app::{APP_DESCRIPTION_KEY, APP_NAME_KEY};
1616
use spin_factor_outbound_http::SelfRequestOrigin;
17+
use spin_factors::RuntimeFactors;
1718
use spin_http::{
1819
app_info::AppInfo,
1920
body,
@@ -40,27 +41,27 @@ use crate::{
4041
};
4142

4243
/// An HTTP server which runs Spin apps.
43-
pub struct HttpServer {
44+
pub struct HttpServer<F: RuntimeFactors> {
4445
/// The address the server is listening on.
4546
listen_addr: SocketAddr,
4647
/// The TLS configuration for the server.
4748
tls_config: Option<TlsConfig>,
4849
/// Request router.
4950
router: Router,
5051
/// The app being triggered.
51-
trigger_app: TriggerApp,
52+
trigger_app: TriggerApp<F>,
5253
// Component ID -> component trigger config
5354
component_trigger_configs: HashMap<String, HttpTriggerConfig>,
5455
// Component ID -> handler type
5556
component_handler_types: HashMap<String, HandlerType>,
5657
}
5758

58-
impl HttpServer {
59+
impl<F: RuntimeFactors> HttpServer<F> {
5960
/// Create a new [`HttpServer`].
6061
pub fn new(
6162
listen_addr: SocketAddr,
6263
tls_config: Option<TlsConfig>,
63-
trigger_app: TriggerApp,
64+
trigger_app: TriggerApp<F>,
6465
) -> anyhow::Result<Self> {
6566
// This needs to be a vec before building the router to handle duplicate routes
6667
let component_trigger_configs = Vec::from_iter(
@@ -235,10 +236,11 @@ impl HttpServer {
235236
let mut instance_builder = self.trigger_app.prepare(component_id)?;
236237

237238
// Set up outbound HTTP request origin and service chaining
238-
let outbound_http = instance_builder.factor_builders().outbound_http();
239-
let origin = SelfRequestOrigin::create(server_scheme, &self.listen_addr)?;
240-
outbound_http.set_self_request_origin(origin);
241-
outbound_http.set_request_interceptor(OutboundHttpInterceptor::new(self.clone()))?;
239+
// TODO: bring back once outbound networking is refactored
240+
// let outbound_http = instance_builder.factor_builders().outbound_http();
241+
// let origin = SelfRequestOrigin::create(server_scheme, &self.listen_addr)?;
242+
// outbound_http.set_self_request_origin(origin);
243+
// outbound_http.set_request_interceptor(OutboundHttpInterceptor::new(self.clone()))?;
242244

243245
// Prepare HTTP executor
244246
let trigger_config = self.component_trigger_configs.get(component_id).unwrap();
@@ -448,9 +450,9 @@ fn set_req_uri(req: &mut Request<Body>, scheme: Scheme) -> anyhow::Result<()> {
448450

449451
/// An HTTP executor.
450452
pub(crate) trait HttpExecutor: Clone + Send + Sync + 'static {
451-
fn execute(
453+
fn execute<F: RuntimeFactors>(
452454
&self,
453-
instance_builder: TriggerInstanceBuilder,
455+
instance_builder: TriggerInstanceBuilder<F>,
454456
route_match: &RouteMatch,
455457
req: Request<Body>,
456458
client_addr: SocketAddr,

crates/trigger-http/src/spin.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::net::SocketAddr;
33
use anyhow::Result;
44
use http_body_util::BodyExt;
55
use hyper::{Request, Response};
6+
use spin_factors::RuntimeFactors;
67
use spin_http::body;
78
use spin_http::routes::RouteMatch;
89
use spin_world::v1::http_types;
@@ -20,9 +21,9 @@ pub struct SpinHttpExecutor;
2021

2122
impl HttpExecutor for SpinHttpExecutor {
2223
#[instrument(name = "spin_trigger_http.execute_wasm", skip_all, err(level = Level::INFO), fields(otel.name = format!("execute_wasm_component {}", route_match.component_id())))]
23-
async fn execute(
24+
async fn execute<F: RuntimeFactors>(
2425
&self,
25-
instance_builder: TriggerInstanceBuilder<'_>,
26+
instance_builder: TriggerInstanceBuilder<'_, F>,
2627
route_match: &RouteMatch,
2728
req: Request<Body>,
2829
client_addr: SocketAddr,

crates/trigger-http/src/wagi.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{io::Cursor, net::SocketAddr};
33
use anyhow::{ensure, Result};
44
use http_body_util::BodyExt;
55
use hyper::{Request, Response};
6+
use spin_factors::RuntimeFactors;
67
use spin_http::{config::WagiTriggerConfig, routes::RouteMatch, wagi};
78
use tracing::{instrument, Level};
89
use wasmtime_wasi::pipe::MemoryOutputPipe;
@@ -17,9 +18,9 @@ pub struct WagiHttpExecutor {
1718

1819
impl HttpExecutor for WagiHttpExecutor {
1920
#[instrument(name = "spin_trigger_http.execute_wagi", skip_all, err(level = Level::INFO), fields(otel.name = format!("execute_wagi_component {}", route_match.component_id())))]
20-
async fn execute(
21+
async fn execute<F: RuntimeFactors>(
2122
&self,
22-
mut instance_builder: TriggerInstanceBuilder<'_>,
23+
mut instance_builder: TriggerInstanceBuilder<'_, F>,
2324
route_match: &RouteMatch,
2425
req: Request<Body>,
2526
client_addr: SocketAddr,
@@ -73,13 +74,14 @@ impl HttpExecutor for WagiHttpExecutor {
7374

7475
let stdout = MemoryOutputPipe::new(usize::MAX);
7576

76-
let wasi_builder = instance_builder.factor_builders().wasi();
77+
// TODO:
78+
// let wasi_builder = instance_builder.factor_builders().wasi();
7779

78-
// Set up Wagi environment
79-
wasi_builder.args(argv.split(' '));
80-
wasi_builder.env(headers);
81-
wasi_builder.stdin_pipe(Cursor::new(body));
82-
wasi_builder.stdout(stdout.clone());
80+
// // Set up Wagi environment
81+
// wasi_builder.args(argv.split(' '));
82+
// wasi_builder.env(headers);
83+
// wasi_builder.stdin_pipe(Cursor::new(body));
84+
// wasi_builder.stdout(stdout.clone());
8385

8486
let (instance, mut store) = instance_builder.instantiate(()).await?;
8587

crates/trigger-http/src/wasi.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use http::{HeaderName, HeaderValue};
66
use hyper::{Request, Response};
77
use spin_factor_outbound_http::wasi_2023_10_18::exports::wasi::http::incoming_handler::Guest as IncomingHandler2023_10_18;
88
use spin_factor_outbound_http::wasi_2023_11_10::exports::wasi::http::incoming_handler::Guest as IncomingHandler2023_11_10;
9+
use spin_factors::RuntimeFactors;
910
use spin_http::routes::RouteMatch;
1011
use tokio::{sync::oneshot, task};
1112
use tracing::{instrument, Instrument, Level};
@@ -25,9 +26,9 @@ pub struct WasiHttpExecutor {
2526

2627
impl HttpExecutor for WasiHttpExecutor {
2728
#[instrument(name = "spin_trigger_http.execute_wasm", skip_all, err(level = Level::INFO), fields(otel.name = format!("execute_wasm_component {}", route_match.component_id())))]
28-
async fn execute(
29+
async fn execute<F: RuntimeFactors>(
2930
&self,
30-
instance_builder: TriggerInstanceBuilder<'_>,
31+
instance_builder: TriggerInstanceBuilder<'_, F>,
3132
route_match: &RouteMatch,
3233
mut req: Request<Body>,
3334
client_addr: SocketAddr,

0 commit comments

Comments
 (0)