Skip to content

Commit

Permalink
Merge pull request #2790 from fermyon/generic-factors2
Browse files Browse the repository at this point in the history
Make `Trigger` trait generic over `RuntimeFactors`
  • Loading branch information
rylev authored Sep 4, 2024
2 parents 855572b + 786e803 commit 3e62d2e
Show file tree
Hide file tree
Showing 48 changed files with 859 additions and 655 deletions.
96 changes: 38 additions & 58 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ spin-locked-app = { path = "crates/locked-app" }
spin-manifest = { path = "crates/manifest" }
spin-oci = { path = "crates/oci" }
spin-plugins = { path = "crates/plugins" }
spin-runtime-factors = { path = "crates/runtime-factors" }
spin-telemetry = { path = "crates/telemetry", features = [
"tracing-log-compat",
] }
Expand All @@ -66,7 +67,7 @@ terminal = { path = "crates/terminal" }
subprocess = "0.2.9"
tempfile = "3.8.0"
tokio = { version = "1.23", features = ["full"] }
toml = "0.6"
toml = "0.8"
tracing = { workspace = true }
url = "2.2.2"
uuid = { version = "^1.0", features = ["v4"] }
Expand Down Expand Up @@ -109,9 +110,9 @@ vergen = { version = "^8.2.1", default-features = false, features = [
default = ["llm"]
all-tests = ["extern-dependencies-tests"]
extern-dependencies-tests = []
llm = ["spin-trigger-http/llm"]
llm-metal = ["llm", "spin-trigger-http/llm-metal"]
llm-cublas = ["llm", "spin-trigger-http/llm-cublas"]
llm = ["spin-runtime-factors/llm"]
llm-metal = ["llm", "spin-runtime-factors/llm-metal"]
llm-cublas = ["llm", "spin-runtime-factors/llm-cublas"]

[workspace]
members = [
Expand Down
11 changes: 7 additions & 4 deletions crates/factor-key-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::{
use anyhow::ensure;
use host::KEY_VALUE_STORES_KEY;
use spin_factors::{
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders,
PrepareContext, RuntimeFactors,
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext, RuntimeFactors,
};
use util::{CachingStoreManager, DefaultManagerGetter};

Expand Down Expand Up @@ -89,8 +88,7 @@ impl Factor for KeyValueFactor {

fn prepare<T: RuntimeFactors>(
&self,
ctx: PrepareContext<Self>,
_builders: &mut InstanceBuilders<T>,
ctx: PrepareContext<T, Self>,
) -> anyhow::Result<InstanceBuilder> {
let app_state = ctx.app_state();
let allowed_stores = app_state
Expand Down Expand Up @@ -133,6 +131,11 @@ impl AppState {
.values()
.any(|stores| stores.contains(label))
}

/// Get a store by label.
pub async fn get_store(&self, label: &str) -> Option<Arc<dyn Store>> {
self.store_manager.get(label).await.ok()
}
}

pub struct InstanceBuilder {
Expand Down
6 changes: 2 additions & 4 deletions crates/factor-llm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use spin_factors::{
ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
SelfInstanceBuilder,
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
};
use spin_locked_app::MetadataKey;
use spin_world::v1::llm::{self as v1};
Expand Down Expand Up @@ -77,8 +76,7 @@ impl Factor for LlmFactor {

fn prepare<T: RuntimeFactors>(
&self,
ctx: PrepareContext<Self>,
_builders: &mut InstanceBuilders<T>,
ctx: PrepareContext<T, Self>,
) -> anyhow::Result<Self::InstanceBuilder> {
let allowed_models = ctx
.app_state()
Expand Down
8 changes: 3 additions & 5 deletions crates/factor-outbound-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use spin_factor_outbound_networking::{
ComponentTlsConfigs, OutboundAllowedHosts, OutboundNetworkingFactor,
};
use spin_factors::{
anyhow, ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
SelfInstanceBuilder,
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
};
use wasmtime_wasi_http::WasiHttpCtx;

Expand Down Expand Up @@ -59,10 +58,9 @@ impl Factor for OutboundHttpFactor {

fn prepare<T: RuntimeFactors>(
&self,
_ctx: PrepareContext<Self>,
builders: &mut InstanceBuilders<T>,
mut ctx: PrepareContext<T, Self>,
) -> anyhow::Result<Self::InstanceBuilder> {
let outbound_networking = builders.get_mut::<OutboundNetworkingFactor>()?;
let outbound_networking = ctx.instance_builder::<OutboundNetworkingFactor>()?;
let allowed_hosts = outbound_networking.allowed_hosts();
let component_tls_configs = outbound_networking.component_tls_configs().clone();
Ok(InstanceState {
Expand Down
10 changes: 4 additions & 6 deletions crates/factor-outbound-mqtt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use rumqttc::{AsyncClient, Event, Incoming, Outgoing, QoS};
use spin_core::async_trait;
use spin_factor_outbound_networking::OutboundNetworkingFactor;
use spin_factors::{
ConfigureAppContext, Factor, InstanceBuilders, PrepareContext, RuntimeFactors,
SelfInstanceBuilder,
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
};
use spin_world::v2::mqtt::{self as v2, Error, Qos};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -49,11 +48,10 @@ impl Factor for OutboundMqttFactor {

fn prepare<T: RuntimeFactors>(
&self,
_ctx: PrepareContext<Self>,
builders: &mut InstanceBuilders<T>,
mut ctx: PrepareContext<T, Self>,
) -> anyhow::Result<Self::InstanceBuilder> {
let allowed_hosts = builders
.get_mut::<OutboundNetworkingFactor>()?
let allowed_hosts = ctx
.instance_builder::<OutboundNetworkingFactor>()?
.allowed_hosts();
Ok(InstanceState::new(
allowed_hosts,
Expand Down
7 changes: 3 additions & 4 deletions crates/factor-outbound-mysql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {

fn prepare<T: spin_factors::RuntimeFactors>(
&self,
_ctx: spin_factors::PrepareContext<Self>,
builders: &mut spin_factors::InstanceBuilders<T>,
mut ctx: spin_factors::PrepareContext<T, Self>,
) -> anyhow::Result<Self::InstanceBuilder> {
let allowed_hosts = builders
.get_mut::<OutboundNetworkingFactor>()?
let allowed_hosts = ctx
.instance_builder::<OutboundNetworkingFactor>()?
.allowed_hosts();
Ok(InstanceState {
allowed_hosts,
Expand Down
12 changes: 5 additions & 7 deletions crates/factor-outbound-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use spin_factor_variables::VariablesFactor;
use spin_factor_wasi::{SocketAddrUse, WasiFactor};
use spin_factors::{
anyhow::{self, Context},
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, InstanceBuilders, PrepareContext,
RuntimeFactors,
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, PrepareContext, RuntimeFactors,
};

pub use config::{
Expand Down Expand Up @@ -82,17 +81,16 @@ impl Factor for OutboundNetworkingFactor {

fn prepare<T: RuntimeFactors>(
&self,
ctx: PrepareContext<Self>,
builders: &mut InstanceBuilders<T>,
mut ctx: PrepareContext<T, Self>,
) -> anyhow::Result<Self::InstanceBuilder> {
let hosts = ctx
.app_state()
.component_allowed_hosts
.get(ctx.app_component().id())
.cloned()
.context("missing component allowed hosts")?;
let resolver = builders
.get_mut::<VariablesFactor>()?
let resolver = ctx
.instance_builder::<VariablesFactor>()?
.expression_resolver()
.clone();
let allowed_hosts_future = async move {
Expand All @@ -103,7 +101,7 @@ impl Factor for OutboundNetworkingFactor {
.boxed()
.shared();

match builders.get_mut::<WasiFactor>() {
match ctx.instance_builder::<WasiFactor>() {
Ok(wasi_builder) => {
// Update Wasi socket allowed ports
let allowed_hosts = OutboundAllowedHosts {
Expand Down
Loading

0 comments on commit 3e62d2e

Please sign in to comment.