Skip to content

Commit 3e62d2e

Browse files
authored
Merge pull request #2790 from fermyon/generic-factors2
Make `Trigger` trait generic over `RuntimeFactors`
2 parents 855572b + 786e803 commit 3e62d2e

File tree

48 files changed

+859
-655
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+859
-655
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ spin-locked-app = { path = "crates/locked-app" }
5454
spin-manifest = { path = "crates/manifest" }
5555
spin-oci = { path = "crates/oci" }
5656
spin-plugins = { path = "crates/plugins" }
57+
spin-runtime-factors = { path = "crates/runtime-factors" }
5758
spin-telemetry = { path = "crates/telemetry", features = [
5859
"tracing-log-compat",
5960
] }
@@ -66,7 +67,7 @@ terminal = { path = "crates/terminal" }
6667
subprocess = "0.2.9"
6768
tempfile = "3.8.0"
6869
tokio = { version = "1.23", features = ["full"] }
69-
toml = "0.6"
70+
toml = "0.8"
7071
tracing = { workspace = true }
7172
url = "2.2.2"
7273
uuid = { version = "^1.0", features = ["v4"] }
@@ -109,9 +110,9 @@ vergen = { version = "^8.2.1", default-features = false, features = [
109110
default = ["llm"]
110111
all-tests = ["extern-dependencies-tests"]
111112
extern-dependencies-tests = []
112-
llm = ["spin-trigger-http/llm"]
113-
llm-metal = ["llm", "spin-trigger-http/llm-metal"]
114-
llm-cublas = ["llm", "spin-trigger-http/llm-cublas"]
113+
llm = ["spin-runtime-factors/llm"]
114+
llm-metal = ["llm", "spin-runtime-factors/llm-metal"]
115+
llm-cublas = ["llm", "spin-runtime-factors/llm-cublas"]
115116

116117
[workspace]
117118
members = [

crates/factor-key-value/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use std::{
1010
use anyhow::ensure;
1111
use host::KEY_VALUE_STORES_KEY;
1212
use spin_factors::{
13-
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders,
14-
PrepareContext, RuntimeFactors,
13+
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext, RuntimeFactors,
1514
};
1615
use util::{CachingStoreManager, DefaultManagerGetter};
1716

@@ -89,8 +88,7 @@ impl Factor for KeyValueFactor {
8988

9089
fn prepare<T: RuntimeFactors>(
9190
&self,
92-
ctx: PrepareContext<Self>,
93-
_builders: &mut InstanceBuilders<T>,
91+
ctx: PrepareContext<T, Self>,
9492
) -> anyhow::Result<InstanceBuilder> {
9593
let app_state = ctx.app_state();
9694
let allowed_stores = app_state
@@ -133,6 +131,11 @@ impl AppState {
133131
.values()
134132
.any(|stores| stores.contains(label))
135133
}
134+
135+
/// Get a store by label.
136+
pub async fn get_store(&self, label: &str) -> Option<Arc<dyn Store>> {
137+
self.store_manager.get(label).await.ok()
138+
}
136139
}
137140

138141
pub struct InstanceBuilder {

crates/factor-llm/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::sync::Arc;
66

77
use async_trait::async_trait;
88
use spin_factors::{
9-
ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
10-
SelfInstanceBuilder,
9+
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1110
};
1211
use spin_locked_app::MetadataKey;
1312
use spin_world::v1::llm::{self as v1};
@@ -77,8 +76,7 @@ impl Factor for LlmFactor {
7776

7877
fn prepare<T: RuntimeFactors>(
7978
&self,
80-
ctx: PrepareContext<Self>,
81-
_builders: &mut InstanceBuilders<T>,
79+
ctx: PrepareContext<T, Self>,
8280
) -> anyhow::Result<Self::InstanceBuilder> {
8381
let allowed_models = ctx
8482
.app_state()

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use spin_factor_outbound_networking::{
1414
ComponentTlsConfigs, OutboundAllowedHosts, OutboundNetworkingFactor,
1515
};
1616
use spin_factors::{
17-
anyhow, ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
18-
SelfInstanceBuilder,
17+
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1918
};
2019
use wasmtime_wasi_http::WasiHttpCtx;
2120

@@ -59,10 +58,9 @@ impl Factor for OutboundHttpFactor {
5958

6059
fn prepare<T: RuntimeFactors>(
6160
&self,
62-
_ctx: PrepareContext<Self>,
63-
builders: &mut InstanceBuilders<T>,
61+
mut ctx: PrepareContext<T, Self>,
6462
) -> anyhow::Result<Self::InstanceBuilder> {
65-
let outbound_networking = builders.get_mut::<OutboundNetworkingFactor>()?;
63+
let outbound_networking = ctx.instance_builder::<OutboundNetworkingFactor>()?;
6664
let allowed_hosts = outbound_networking.allowed_hosts();
6765
let component_tls_configs = outbound_networking.component_tls_configs().clone();
6866
Ok(InstanceState {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use rumqttc::{AsyncClient, Event, Incoming, Outgoing, QoS};
99
use spin_core::async_trait;
1010
use spin_factor_outbound_networking::OutboundNetworkingFactor;
1111
use spin_factors::{
12-
ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
13-
SelfInstanceBuilder,
12+
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1413
};
1514
use spin_world::v2::mqtt::{self as v2, Error, Qos};
1615
use tokio::sync::Mutex;
@@ -49,11 +48,10 @@ impl Factor for OutboundMqttFactor {
4948

5049
fn prepare<T: RuntimeFactors>(
5150
&self,
52-
_ctx: PrepareContext<Self>,
53-
builders: &mut InstanceBuilders<T>,
51+
mut ctx: PrepareContext<T, Self>,
5452
) -> anyhow::Result<Self::InstanceBuilder> {
55-
let allowed_hosts = builders
56-
.get_mut::<OutboundNetworkingFactor>()?
53+
let allowed_hosts = ctx
54+
.instance_builder::<OutboundNetworkingFactor>()?
5755
.allowed_hosts();
5856
Ok(InstanceState::new(
5957
allowed_hosts,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {
3232

3333
fn prepare<T: spin_factors::RuntimeFactors>(
3434
&self,
35-
_ctx: spin_factors::PrepareContext<Self>,
36-
builders: &mut spin_factors::InstanceBuilders<T>,
35+
mut ctx: spin_factors::PrepareContext<T, Self>,
3736
) -> anyhow::Result<Self::InstanceBuilder> {
38-
let allowed_hosts = builders
39-
.get_mut::<OutboundNetworkingFactor>()?
37+
let allowed_hosts = ctx
38+
.instance_builder::<OutboundNetworkingFactor>()?
4039
.allowed_hosts();
4140
Ok(InstanceState {
4241
allowed_hosts,

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use spin_factor_variables::VariablesFactor;
1313
use spin_factor_wasi::{SocketAddrUse, WasiFactor};
1414
use spin_factors::{
1515
anyhow::{self, Context},
16-
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, InstanceBuilders, PrepareContext,
17-
RuntimeFactors,
16+
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, PrepareContext, RuntimeFactors,
1817
};
1918

2019
pub use config::{
@@ -82,17 +81,16 @@ impl Factor for OutboundNetworkingFactor {
8281

8382
fn prepare<T: RuntimeFactors>(
8483
&self,
85-
ctx: PrepareContext<Self>,
86-
builders: &mut InstanceBuilders<T>,
84+
mut ctx: PrepareContext<T, Self>,
8785
) -> anyhow::Result<Self::InstanceBuilder> {
8886
let hosts = ctx
8987
.app_state()
9088
.component_allowed_hosts
9189
.get(ctx.app_component().id())
9290
.cloned()
9391
.context("missing component allowed hosts")?;
94-
let resolver = builders
95-
.get_mut::<VariablesFactor>()?
92+
let resolver = ctx
93+
.instance_builder::<VariablesFactor>()?
9694
.expression_resolver()
9795
.clone();
9896
let allowed_hosts_future = async move {
@@ -103,7 +101,7 @@ impl Factor for OutboundNetworkingFactor {
103101
.boxed()
104102
.shared();
105103

106-
match builders.get_mut::<WasiFactor>() {
104+
match ctx.instance_builder::<WasiFactor>() {
107105
Ok(wasi_builder) => {
108106
// Update Wasi socket allowed ports
109107
let allowed_hosts = OutboundAllowedHosts {

0 commit comments

Comments
 (0)