Skip to content

Commit

Permalink
Merge pull request #763 from lann/spin-core-refactor
Browse files Browse the repository at this point in the history
Spin core refactor
  • Loading branch information
lann authored Sep 29, 2022
2 parents e4245de + c12caa5 commit dfb3a90
Show file tree
Hide file tree
Showing 67 changed files with 3,701 additions and 2,240 deletions.
358 changes: 147 additions & 211 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ serde_json = "1.0.82"
sha2 = "0.10.2"
spin-build = { path = "crates/build" }
spin-config = { path = "crates/config" }
spin-engine = { path = "crates/engine" }
spin-http = { path = "crates/http" }
spin-loader = { path = "crates/loader" }
spin-manifest = { path = "crates/manifest" }
Expand Down Expand Up @@ -71,9 +70,10 @@ e2e-tests = []
[workspace]
members = [
"crates/abi-conformance",
"crates/app",
"crates/build",
"crates/config",
"crates/engine",
"crates/core",
"crates/http",
"crates/loader",
"crates/manifest",
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ check-rust-examples:

.PHONY: test-unit
test-unit:
RUST_LOG=$(LOG_LEVEL) cargo test --all --no-fail-fast -- --skip integration_tests --nocapture --include-ignored
RUST_LOG=$(LOG_LEVEL) cargo test --all --no-fail-fast -- --skip integration_tests --nocapture

.PHONY: test-integration
test-integration:
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --no-fail-fast -- --nocapture --include-ignored
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --no-fail-fast -- --nocapture

.PHONY: test-e2e
test-e2e:
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- integration_tests::test_dependencies --nocapture
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- --skip integration_tests::test_dependencies --nocapture --include-ignored
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features e2e-tests --no-fail-fast -- --skip integration_tests::test_dependencies --nocapture

.PHONY: test-sdk-go
test-sdk-go:
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ error: the `wasm32-wasi` target is not installed

std::fs::create_dir_all("target/test-programs").unwrap();

build_wasm_test_program("core-wasi-test.wasm", "crates/core/tests/core-wasi-test");
build_wasm_test_program("rust-http-test.wasm", "crates/http/tests/rust-http-test");
build_wasm_test_program("redis-rust.wasm", "crates/redis/tests/rust");
build_wasm_test_program("wagi-test.wasm", "crates/http/tests/wagi-test");
Expand Down
13 changes: 13 additions & 0 deletions crates/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "spin-app"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
ouroboros = "0.15"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
spin-core = { path = "../core" }
thiserror = "1.0"
59 changes: 59 additions & 0 deletions crates/app/src/host_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::sync::Arc;

use spin_core::{EngineBuilder, HostComponent, HostComponentsData};

use crate::AppComponent;

/// A trait for "dynamic" Spin host components.
///
/// This extends [`HostComponent`] to support per-[`AppComponent`] dynamic
/// runtime configuration.
pub trait DynamicHostComponent: HostComponent {
/// Called on [`AppComponent`] instance initialization.
///
/// The `data` returned by [`HostComponent::build_data`] is passed, along
/// with a reference to the `component` being instantiated.
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()>;
}

impl<DHC: DynamicHostComponent> DynamicHostComponent for Arc<DHC> {
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()> {
(**self).update_data(data, component)
}
}

type DataUpdater =
Box<dyn Fn(&mut HostComponentsData, &AppComponent) -> anyhow::Result<()> + Send + Sync>;

#[derive(Default)]
pub struct DynamicHostComponents {
data_updaters: Vec<DataUpdater>,
}

impl DynamicHostComponents {
pub fn add_dynamic_host_component<T: Send + Sync, DHC: DynamicHostComponent>(
&mut self,
engine_builder: &mut EngineBuilder<T>,
host_component: DHC,
) -> anyhow::Result<()> {
let host_component = Arc::new(host_component);
let handle = engine_builder.add_host_component(host_component.clone())?;
self.data_updaters
.push(Box::new(move |host_components_data, component| {
let data = host_components_data.get_or_insert(handle);
host_component.update_data(data, component)
}));
Ok(())
}

pub fn update_data(
&self,
host_components_data: &mut HostComponentsData,
component: &AppComponent,
) -> anyhow::Result<()> {
for data_updater in &self.data_updaters {
data_updater(host_components_data, component)?;
}
Ok(())
}
}
Loading

0 comments on commit dfb3a90

Please sign in to comment.