Skip to content

Commit 4f392b0

Browse files
committed
Make ConnectionCreator async and take label
Signed-off-by: Ryan Levick <[email protected]>
1 parent a2333d3 commit 4f392b0

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

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
}

0 commit comments

Comments
 (0)