Skip to content

Commit ba32722

Browse files
authored
Merge pull request #2807 from fermyon/small-changes
A collection of small changes
2 parents ee08554 + 76e7c2d commit ba32722

File tree

13 files changed

+89
-37
lines changed

13 files changed

+89
-37
lines changed

crates/app/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub const APP_DESCRIPTION_KEY: MetadataKey = MetadataKey::new("description");
2828
pub const OCI_IMAGE_DIGEST_KEY: MetadataKey = MetadataKey::new("oci_image_digest");
2929

3030
/// An `App` holds loaded configuration for a Spin application.
31-
#[derive(Debug)]
31+
#[derive(Debug, Clone)]
3232
pub struct App {
3333
id: String,
3434
locked: LockedApp,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use host::KEY_VALUE_STORES_KEY;
1212
use spin_factors::{
1313
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext, RuntimeFactors,
1414
};
15-
use util::{CachingStoreManager, DefaultManagerGetter};
15+
use util::DefaultManagerGetter;
1616

1717
pub use host::{log_error, Error, KeyValueDispatch, Store, StoreManager};
1818
pub use runtime_config::RuntimeConfig;
19-
pub use util::DelegatingStoreManager;
19+
pub use util::{CachingStoreManager, DelegatingStoreManager};
2020

2121
/// A factor that provides key-value storage.
2222
pub struct KeyValueFactor {

crates/factor-sqlite/src/host.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ impl v2::HostConnection for InstanceState {
6868
}
6969
let conn = (self.get_connection_creator)(&database)
7070
.ok_or(v2::Error::NoSuchDatabase)?
71-
.create_connection()?;
71+
.create_connection(&database)
72+
.await?;
7273
self.connections
7374
.push(conn)
7475
.map_err(|()| v2::Error::Io("too many connections opened".to_string()))

crates/factor-sqlite/src/lib.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,13 @@ impl AppState {
168168
/// Get a connection for a given database label.
169169
///
170170
/// Returns `None` if there is no connection creator for the given label.
171-
pub fn get_connection(&self, label: &str) -> Option<Result<Box<dyn Connection>, v2::Error>> {
172-
let connection = (self.get_connection_creator)(label)?.create_connection();
171+
pub async fn get_connection(
172+
&self,
173+
label: &str,
174+
) -> Option<Result<Box<dyn Connection>, v2::Error>> {
175+
let connection = (self.get_connection_creator)(label)?
176+
.create_connection(label)
177+
.await;
173178
Some(connection)
174179
}
175180

@@ -182,18 +187,27 @@ impl AppState {
182187
}
183188

184189
/// A creator of a connections for a particular SQLite database.
190+
#[async_trait]
185191
pub trait ConnectionCreator: Send + Sync {
186192
/// Get a *new* [`Connection`]
187193
///
188194
/// The connection should be a new connection, not a reused one.
189-
fn create_connection(&self) -> Result<Box<dyn Connection + 'static>, v2::Error>;
195+
async fn create_connection(
196+
&self,
197+
label: &str,
198+
) -> Result<Box<dyn Connection + 'static>, v2::Error>;
190199
}
191200

201+
#[async_trait]
192202
impl<F> ConnectionCreator for F
193203
where
194204
F: Fn() -> anyhow::Result<Box<dyn Connection + 'static>> + Send + Sync + 'static,
195205
{
196-
fn create_connection(&self) -> Result<Box<dyn Connection + 'static>, v2::Error> {
206+
async fn create_connection(
207+
&self,
208+
label: &str,
209+
) -> Result<Box<dyn Connection + 'static>, v2::Error> {
210+
let _ = label;
197211
(self)().map_err(|_| v2::Error::InvalidConnection)
198212
}
199213
}

crates/factor-sqlite/tests/factor_test.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use spin_factors::{
88
};
99
use spin_factors_test::{toml, TestEnvironment};
1010
use spin_sqlite::RuntimeConfigResolver;
11+
use spin_world::async_trait;
1112

1213
#[derive(RuntimeFactors)]
1314
struct TestFactors {
@@ -143,11 +144,14 @@ impl spin_factor_sqlite::DefaultLabelResolver for DefaultLabelResolver {
143144
/// A connection creator that always returns an error.
144145
struct InvalidConnectionCreator;
145146

147+
#[async_trait]
146148
impl spin_factor_sqlite::ConnectionCreator for InvalidConnectionCreator {
147-
fn create_connection(
149+
async fn create_connection(
148150
&self,
151+
label: &str,
149152
) -> Result<Box<dyn spin_factor_sqlite::Connection + 'static>, spin_world::v2::sqlite::Error>
150153
{
154+
let _ = label;
151155
Err(spin_world::v2::sqlite::Error::InvalidConnection)
152156
}
153157
}

crates/factor-variables/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ pub struct VariablesFactor {
1717
_priv: (),
1818
}
1919

20+
impl VariablesFactor {
21+
/// Creates a new `VariablesFactor`.
22+
pub fn new() -> Self {
23+
Default::default()
24+
}
25+
}
26+
2027
impl Factor for VariablesFactor {
2128
type RuntimeConfig = RuntimeConfig;
2229
type AppState = AppState;
@@ -43,7 +50,8 @@ impl Factor for VariablesFactor {
4350
)?;
4451
}
4552

46-
for provider in ctx.take_runtime_config().unwrap_or_default() {
53+
let providers = ctx.take_runtime_config().unwrap_or_default();
54+
for provider in providers {
4755
expression_resolver.add_provider(provider);
4856
}
4957

crates/factors-executor/src/lib.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, sync::Arc};
22

33
use anyhow::Context;
44
use spin_app::{App, AppComponent};
@@ -35,10 +35,12 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
3535
hooks: Default::default(),
3636
})
3737
}
38-
}
3938

40-
impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
41-
/// Adds the given [`ExecutorHooks`] to this executor.
39+
pub fn core_engine(&self) -> &spin_core::Engine<InstanceState<T::InstanceState, U>> {
40+
&self.core_engine
41+
}
42+
43+
// Adds the given [`ExecutorHooks`] to this executor.
4244
///
4345
/// Hooks are run in the order they are added.
4446
pub fn add_hooks(&mut self, hooks: impl ExecutorHooks<T, U> + 'static) {
@@ -47,17 +49,17 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
4749

4850
/// Loads a [`FactorsApp`] with this executor.
4951
pub async fn load_app(
50-
mut self,
52+
self: Arc<Self>,
5153
app: App,
5254
runtime_config: T::RuntimeConfig,
53-
mut component_loader: impl ComponentLoader,
55+
component_loader: &impl ComponentLoader,
5456
) -> anyhow::Result<FactorsExecutorApp<T, U>> {
5557
let configured_app = self
5658
.factors
5759
.configure_app(app, runtime_config)
5860
.context("failed to configure app")?;
5961

60-
for hooks in &mut self.hooks {
62+
for hooks in &self.hooks {
6163
hooks.configure_app(&configured_app).await?;
6264
}
6365

@@ -73,7 +75,7 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
7375
}
7476

7577
Ok(FactorsExecutorApp {
76-
executor: self,
78+
executor: self.clone(),
7779
configured_app,
7880
component_instance_pres,
7981
})
@@ -86,7 +88,7 @@ where
8688
T: RuntimeFactors,
8789
{
8890
/// Configure app hooks run immediately after [`RuntimeFactors::configure_app`].
89-
async fn configure_app(&mut self, configured_app: &ConfiguredApp<T>) -> anyhow::Result<()> {
91+
async fn configure_app(&self, configured_app: &ConfiguredApp<T>) -> anyhow::Result<()> {
9092
let _ = configured_app;
9193
Ok(())
9294
}
@@ -103,7 +105,7 @@ where
103105
pub trait ComponentLoader {
104106
/// Loads a [`Component`] for the given [`AppComponent`].
105107
async fn load_component(
106-
&mut self,
108+
&self,
107109
engine: &spin_core::wasmtime::Engine,
108110
component: &AppComponent,
109111
) -> anyhow::Result<Component>;
@@ -117,7 +119,7 @@ type InstancePre<T, U> =
117119
/// It is generic over the executor's [`RuntimeFactors`] and any ad-hoc additional
118120
/// per-instance state needed by the caller.
119121
pub struct FactorsExecutorApp<T: RuntimeFactors, U> {
120-
executor: FactorsExecutor<T, U>,
122+
executor: Arc<FactorsExecutor<T, U>>,
121123
configured_app: ConfiguredApp<T>,
122124
// Maps component IDs -> InstancePres
123125
component_instance_pres: HashMap<String, InstancePre<T, U>>,
@@ -249,13 +251,28 @@ impl<T, U> InstanceState<T, U> {
249251
&self.core
250252
}
251253

254+
/// Provides mutable access to the [`spin_core::State`].
255+
pub fn core_state_mut(&mut self) -> &mut spin_core::State {
256+
&mut self.core
257+
}
258+
252259
/// Provides access to the [`RuntimeFactors::InstanceState`].
253-
pub fn factors_instance_state(&mut self) -> &mut T {
260+
pub fn factors_instance_state(&self) -> &T {
261+
&self.factors
262+
}
263+
264+
/// Provides mutable access to the [`RuntimeFactors::InstanceState`].
265+
pub fn factors_instance_state_mut(&mut self) -> &mut T {
254266
&mut self.factors
255267
}
256268

257-
/// Provides access to the `Self::ExecutorInstanceState`.
258-
pub fn executor_instance_state(&mut self) -> &mut U {
269+
/// Provides access to the ad-hoc executor instance state.
270+
pub fn executor_instance_state(&self) -> &U {
271+
&self.executor
272+
}
273+
274+
/// Provides mutable access to the ad-hoc executor instance state.
275+
pub fn executor_instance_state_mut(&mut self) -> &mut U {
259276
&mut self.executor
260277
}
261278
}
@@ -295,10 +312,10 @@ mod tests {
295312
let app = App::new("test-app", locked);
296313

297314
let engine_builder = spin_core::Engine::builder(&Default::default())?;
298-
let executor = FactorsExecutor::new(engine_builder, env.factors)?;
315+
let executor = Arc::new(FactorsExecutor::new(engine_builder, env.factors)?);
299316

300317
let factors_app = executor
301-
.load_app(app, Default::default(), DummyComponentLoader)
318+
.load_app(app, Default::default(), &DummyComponentLoader)
302319
.await?;
303320

304321
let mut instance_builder = factors_app.prepare("empty")?;
@@ -321,7 +338,7 @@ mod tests {
321338
#[async_trait]
322339
impl ComponentLoader for DummyComponentLoader {
323340
async fn load_component(
324-
&mut self,
341+
&self,
325342
engine: &spin_core::wasmtime::Engine,
326343
_component: &AppComponent,
327344
) -> anyhow::Result<Component> {

crates/trigger-http/src/wasi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl HttpExecutor for WasiHttpExecutor {
5353
}));
5454

5555
let mut wasi_http = spin_factor_outbound_http::OutboundHttpFactor::get_wasi_http_impl(
56-
store.data_mut().factors_instance_state(),
56+
store.data_mut().factors_instance_state_mut(),
5757
)
5858
.context("missing OutboundHttpFactor")?;
5959

crates/trigger/src/cli.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod sqlite_statements;
44
mod stdio;
55
mod summary;
66

7-
use std::future::Future;
87
use std::path::PathBuf;
8+
use std::{future::Future, sync::Arc};
99

1010
use anyhow::{Context, Result};
1111
use clap::{Args, IntoApp, Parser};
@@ -345,7 +345,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
345345
#[async_trait]
346346
impl ComponentLoader for SimpleComponentLoader {
347347
async fn load_component(
348-
&mut self,
348+
&self,
349349
engine: &spin_core::wasmtime::Engine,
350350
component: &spin_factors::AppComponent,
351351
) -> anyhow::Result<spin_core::Component> {
@@ -373,11 +373,12 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
373373

374374
let mut executor = FactorsExecutor::new(core_engine_builder, factors)?;
375375
B::configure_app(&mut executor, &runtime_config, &common_options, &options)?;
376+
let executor = Arc::new(executor);
376377

377378
let configured_app = {
378379
let _sloth_guard = warn_if_wasm_build_slothful();
379380
executor
380-
.load_app(app, runtime_config.into(), SimpleComponentLoader)
381+
.load_app(app, runtime_config.into(), &SimpleComponentLoader)
381382
.await?
382383
};
383384

crates/trigger/src/cli/initial_kv_setter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const DEFAULT_KEY_VALUE_STORE_LABEL: &str = "default";
2020
#[async_trait]
2121
impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for InitialKvSetterHook {
2222
async fn configure_app(
23-
&mut self,
23+
&self,
2424
configured_app: &spin_factors::ConfiguredApp<F>,
2525
) -> anyhow::Result<()> {
2626
if self.kv_pairs.is_empty() {

0 commit comments

Comments
 (0)