Skip to content

Commit cbec0d6

Browse files
committed
factors: Add RuntimeConfig
Also address a bunch of feedback. Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent 1a8be2a commit cbec0d6

File tree

10 files changed

+255
-232
lines changed

10 files changed

+255
-232
lines changed

crates/factor-outbound-networking/src/lib.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ use spin_factor_variables::VariablesFactor;
88
use spin_factor_wasi::WasiFactor;
99
use spin_factors::{
1010
anyhow::{self, Context},
11-
ConfigureAppContext, Factor, InstancePreparers, PrepareContext, RuntimeConfig, RuntimeFactors,
11+
ConfigureAppContext, Factor, FactorInstanceBuilder, InstanceBuilders, PrepareContext,
12+
RuntimeFactors,
1213
};
1314
use spin_outbound_networking::{AllowedHostsConfig, ALLOWED_HOSTS_KEY};
1415

1516
pub struct OutboundNetworkingFactor;
1617

1718
impl Factor for OutboundNetworkingFactor {
19+
type RuntimeConfig = ();
1820
type AppState = AppState;
19-
type InstancePreparer = InstancePreparer;
20-
type InstanceState = ();
21+
type InstanceBuilder = InstanceBuilder;
2122

22-
fn configure_app<Factors: RuntimeFactors>(
23+
fn configure_app<T: RuntimeFactors>(
2324
&self,
24-
ctx: ConfigureAppContext<Factors>,
25-
_runtime_config: &mut impl RuntimeConfig,
25+
ctx: ConfigureAppContext<T, Self>,
2626
) -> anyhow::Result<Self::AppState> {
2727
// Extract allowed_outbound_hosts for all components
2828
let component_allowed_hosts = ctx
@@ -44,17 +44,17 @@ impl Factor for OutboundNetworkingFactor {
4444
})
4545
}
4646

47-
fn create_preparer<T: RuntimeFactors>(
47+
fn prepare<T: RuntimeFactors>(
4848
ctx: PrepareContext<Self>,
49-
mut preparers: InstancePreparers<T>,
50-
) -> anyhow::Result<Self::InstancePreparer> {
49+
builders: &mut InstanceBuilders<T>,
50+
) -> anyhow::Result<Self::InstanceBuilder> {
5151
let hosts = ctx
52-
.app_config()
52+
.app_state()
5353
.component_allowed_hosts
5454
.get(ctx.app_component().id())
5555
.cloned()
5656
.context("missing component allowed hosts")?;
57-
let resolver = preparers.get_mut::<VariablesFactor>()?.resolver().clone();
57+
let resolver = builders.get_mut::<VariablesFactor>()?.resolver().clone();
5858
let allowed_hosts_future = async move {
5959
let prepared = resolver.prepare().await?;
6060
AllowedHostsConfig::parse(&hosts, &prepared)
@@ -69,7 +69,7 @@ impl Factor for OutboundNetworkingFactor {
6969
// )?;
7070

7171
// Update Wasi socket allowed ports
72-
let wasi_preparer = preparers.get_mut::<WasiFactor>()?;
72+
let wasi_preparer = builders.get_mut::<WasiFactor>()?;
7373
let hosts_future = allowed_hosts_future.clone();
7474
wasi_preparer.outbound_socket_addr_check(move |addr| {
7575
let hosts_future = hosts_future.clone();
@@ -87,14 +87,7 @@ impl Factor for OutboundNetworkingFactor {
8787
}
8888
}
8989
});
90-
Ok(InstancePreparer::new(allowed_hosts_future))
91-
}
92-
93-
fn prepare(
94-
&self,
95-
_preparer: InstancePreparer,
96-
) -> anyhow::Result<<OutboundNetworkingFactor as Factor>::InstanceState> {
97-
Ok(())
90+
Ok(InstanceBuilder::new(allowed_hosts_future))
9891
}
9992
}
10093

@@ -106,11 +99,11 @@ pub struct AppState {
10699
type SharedFutureResult<T> = Shared<BoxFuture<'static, Arc<anyhow::Result<T>>>>;
107100

108101
#[derive(Default)]
109-
pub struct InstancePreparer {
102+
pub struct InstanceBuilder {
110103
allowed_hosts_future: Option<SharedFutureResult<AllowedHostsConfig>>,
111104
}
112105

113-
impl InstancePreparer {
106+
impl InstanceBuilder {
114107
fn new(allowed_hosts_future: SharedFutureResult<AllowedHostsConfig>) -> Self {
115108
Self {
116109
allowed_hosts_future: Some(allowed_hosts_future),
@@ -124,3 +117,11 @@ impl InstancePreparer {
124117
.await
125118
}
126119
}
120+
121+
impl FactorInstanceBuilder for InstanceBuilder {
122+
type InstanceState = ();
123+
124+
fn build(self) -> anyhow::Result<Self::InstanceState> {
125+
Ok(())
126+
}
127+
}

crates/factor-variables/src/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use std::sync::Arc;
22

33
use spin_expressions::ProviderResolver;
44
use spin_factors::{
5-
anyhow, ConfigureAppContext, Factor, InitContext, InstancePreparers, PrepareContext,
6-
RuntimeConfig, RuntimeFactors,
5+
anyhow, ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders,
6+
PrepareContext, RuntimeFactors,
77
};
88
use spin_world::{async_trait, v1::config as v1_config, v2::variables};
99

1010
pub struct VariablesFactor;
1111

1212
impl Factor for VariablesFactor {
13+
type RuntimeConfig = ();
1314
type AppState = AppState;
14-
type InstancePreparer = InstancePreparer;
15-
type InstanceState = InstanceState;
15+
type InstanceBuilder = InstanceBuilder;
1616

1717
fn init<Factors: RuntimeFactors>(
1818
&mut self,
@@ -23,10 +23,9 @@ impl Factor for VariablesFactor {
2323
Ok(())
2424
}
2525

26-
fn configure_app<Factors: RuntimeFactors>(
26+
fn configure_app<T: RuntimeFactors>(
2727
&self,
28-
ctx: ConfigureAppContext<Factors>,
29-
_runtime_config: &mut impl RuntimeConfig,
28+
ctx: ConfigureAppContext<T, Self>,
3029
) -> anyhow::Result<Self::AppState> {
3130
let app = ctx.app();
3231
let mut resolver =
@@ -43,41 +42,44 @@ impl Factor for VariablesFactor {
4342
})
4443
}
4544

46-
fn create_preparer<T: RuntimeFactors>(
45+
fn prepare<T: RuntimeFactors>(
4746
ctx: PrepareContext<Self>,
48-
_preparers: InstancePreparers<T>,
49-
) -> anyhow::Result<Self::InstancePreparer> {
47+
_builders: &mut InstanceBuilders<T>,
48+
) -> anyhow::Result<InstanceBuilder> {
5049
let component_id = ctx.app_component().id().to_string();
51-
let resolver = ctx.app_config().resolver.clone();
52-
Ok(InstancePreparer {
50+
let resolver = ctx.app_state().resolver.clone();
51+
Ok(InstanceBuilder {
5352
state: InstanceState {
5453
component_id,
5554
resolver,
5655
},
5756
})
5857
}
59-
60-
fn prepare(&self, preparer: InstancePreparer) -> anyhow::Result<InstanceState> {
61-
Ok(preparer.state)
62-
}
6358
}
6459

6560
#[derive(Default)]
6661
pub struct AppState {
6762
resolver: Arc<ProviderResolver>,
6863
}
6964

70-
#[derive(Default)]
71-
pub struct InstancePreparer {
65+
pub struct InstanceBuilder {
7266
state: InstanceState,
7367
}
7468

75-
impl InstancePreparer {
69+
impl InstanceBuilder {
7670
pub fn resolver(&self) -> &Arc<ProviderResolver> {
7771
&self.state.resolver
7872
}
7973
}
8074

75+
impl FactorInstanceBuilder for InstanceBuilder {
76+
type InstanceState = InstanceState;
77+
78+
fn build(self) -> anyhow::Result<Self::InstanceState> {
79+
Ok(self.state)
80+
}
81+
}
82+
8183
#[derive(Default)]
8284
pub struct InstanceState {
8385
component_id: String,

crates/factor-wasi/src/lib.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ pub mod preview1;
33
use std::{future::Future, net::SocketAddr, path::Path};
44

55
use spin_factors::{
6-
anyhow, AppComponent, Factor, InitContext, InstancePreparers, PrepareContext, RuntimeFactors,
6+
anyhow, AppComponent, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders,
7+
PrepareContext, RuntimeFactors,
78
};
89
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
910

@@ -20,9 +21,9 @@ impl WasiFactor {
2021
}
2122

2223
impl Factor for WasiFactor {
24+
type RuntimeConfig = ();
2325
type AppState = ();
24-
type InstancePreparer = InstancePreparer;
25-
type InstanceState = InstanceState;
26+
type InstanceBuilder = InstanceBuilder;
2627

2728
fn init<Factors: RuntimeFactors>(
2829
&mut self,
@@ -69,10 +70,10 @@ impl Factor for WasiFactor {
6970
Ok(())
7071
}
7172

72-
fn create_preparer<T: RuntimeFactors>(
73+
fn prepare<T: RuntimeFactors>(
7374
ctx: PrepareContext<Self>,
74-
_preparers: InstancePreparers<T>,
75-
) -> anyhow::Result<Self::InstancePreparer> {
75+
_builders: &mut InstanceBuilders<T>,
76+
) -> anyhow::Result<InstanceBuilder> {
7677
let mut wasi_ctx = WasiCtxBuilder::new();
7778

7879
// Apply environment variables
@@ -88,15 +89,7 @@ impl Factor for WasiFactor {
8889
.files_mounter
8990
.mount_files(ctx.app_component(), mount_ctx)?;
9091

91-
Ok(InstancePreparer { wasi_ctx })
92-
}
93-
94-
fn prepare(&self, preparer: InstancePreparer) -> anyhow::Result<InstanceState> {
95-
let InstancePreparer { mut wasi_ctx } = preparer;
96-
Ok(InstanceState {
97-
ctx: wasi_ctx.build(),
98-
table: Default::default(),
99-
})
92+
Ok(InstanceBuilder { wasi_ctx })
10093
}
10194
}
10295

@@ -147,19 +140,23 @@ impl<'a> MountFilesContext<'a> {
147140
}
148141
}
149142

150-
pub struct InstancePreparer {
143+
pub struct InstanceBuilder {
151144
wasi_ctx: WasiCtxBuilder,
152145
}
153146

154-
impl Default for InstancePreparer {
155-
fn default() -> Self {
156-
Self {
157-
wasi_ctx: WasiCtxBuilder::new(),
158-
}
147+
impl FactorInstanceBuilder for InstanceBuilder {
148+
type InstanceState = InstanceState;
149+
150+
fn build(self) -> anyhow::Result<Self::InstanceState> {
151+
let InstanceBuilder { mut wasi_ctx } = self;
152+
Ok(InstanceState {
153+
ctx: wasi_ctx.build(),
154+
table: Default::default(),
155+
})
159156
}
160157
}
161158

162-
impl InstancePreparer {
159+
impl InstanceBuilder {
163160
pub fn outbound_socket_addr_check<F, Fut>(&mut self, check: F)
164161
where
165162
F: Fn(SocketAddr) -> Fut + Send + Sync + Clone + 'static,

crates/factor-wasi/src/preview1.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use spin_factors::{anyhow, Factor, InitContext, RuntimeFactors};
1+
use spin_factors::{anyhow, Factor, FactorInstanceBuilder, InitContext, RuntimeFactors};
22
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
33

44
pub struct WasiPreview1Factor;
55

66
impl Factor for WasiPreview1Factor {
7+
type RuntimeConfig = ();
78
type AppState = ();
8-
type InstancePreparer = InstancePreparer;
9-
type InstanceState = WasiP1Ctx;
9+
type InstanceBuilder = InstanceBuilder;
1010

1111
fn init<Factors: RuntimeFactors>(
1212
&mut self,
@@ -15,19 +15,25 @@ impl Factor for WasiPreview1Factor {
1515
ctx.link_module_bindings(wasmtime_wasi::preview1::add_to_linker_async)
1616
}
1717

18-
fn prepare(&self, mut preparer: InstancePreparer) -> anyhow::Result<WasiP1Ctx> {
19-
Ok(preparer.wasi_ctx.build_p1())
18+
fn prepare<T: RuntimeFactors>(
19+
_ctx: spin_factors::PrepareContext<Self>,
20+
_builders: &mut spin_factors::InstanceBuilders<T>,
21+
) -> anyhow::Result<Self::InstanceBuilder> {
22+
Ok(InstanceBuilder {
23+
wasi_ctx: WasiCtxBuilder::new(),
24+
})
2025
}
2126
}
2227

23-
pub struct InstancePreparer {
28+
pub struct InstanceBuilder {
2429
wasi_ctx: WasiCtxBuilder,
2530
}
2631

27-
impl Default for InstancePreparer {
28-
fn default() -> Self {
29-
Self {
30-
wasi_ctx: WasiCtxBuilder::new(),
31-
}
32+
impl FactorInstanceBuilder for InstanceBuilder {
33+
type InstanceState = WasiP1Ctx;
34+
35+
fn build(self) -> anyhow::Result<Self::InstanceState> {
36+
let Self { mut wasi_ctx } = self;
37+
Ok(wasi_ctx.build_p1())
3238
}
3339
}

0 commit comments

Comments
 (0)