From 4c373e562bde195d7a417ccd38a8a2fe59836e6e Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 18 Mar 2020 13:25:43 -0500 Subject: [PATCH] Perform db action inline --- src/db_actor.rs | 26 ++++++++++++-------------- src/error.rs | 3 +++ src/inbox.rs | 4 ++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/db_actor.rs b/src/db_actor.rs index b5319ae..e9d2af6 100644 --- a/src/db_actor.rs +++ b/src/db_actor.rs @@ -45,34 +45,32 @@ impl Db { Ok(self.actor.send(DbQuery(f)).await?.await?) } - pub fn remove_listener(&self, inbox: XsdAnyUri) { - self.actor.do_send(DbQuery(move |pool: Pool| { + pub async fn remove_listener(&self, inbox: XsdAnyUri) -> Result<(), MyError> { + self.execute_inline(move |pool: Pool| { let inbox = inbox.clone(); async move { let conn = pool.get().await?; - remove_listener(&conn, &inbox).await.map_err(|e| { - error!("Error removing listener, {}", e); - e - }) + remove_listener(&conn, &inbox).await } - })); + }) + .await? + .map_err(MyError::from) } - pub fn add_listener(&self, inbox: XsdAnyUri) { - self.actor.do_send(DbQuery(move |pool: Pool| { + pub async fn add_listener(&self, inbox: XsdAnyUri) -> Result<(), MyError> { + self.execute_inline(move |pool: Pool| { let inbox = inbox.clone(); async move { let conn = pool.get().await?; - add_listener(&conn, &inbox).await.map_err(|e| { - error!("Error adding listener, {}", e); - e - }) + add_listener(&conn, &inbox).await } - })); + }) + .await? + .map_err(MyError::from) } } diff --git a/src/error.rs b/src/error.rs index 1e2f4b8..93f1419 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,6 +8,9 @@ use tokio::sync::oneshot::error::RecvError; #[derive(Debug, thiserror::Error)] pub enum MyError { + #[error("Error in db, {0}")] + DbError(#[from] anyhow::Error), + #[error("Couldn't parse key, {0}")] Key(#[from] KeyError), diff --git a/src/inbox.rs b/src/inbox.rs index 2bec5b4..8b921e7 100644 --- a/src/inbox.rs +++ b/src/inbox.rs @@ -100,7 +100,7 @@ async fn handle_undo( } let inbox = actor.inbox().to_owned(); - db.remove_listener(inbox); + db.remove_listener(inbox).await?; let undo = generate_undo_follow(state, &actor.id, &my_id)?; @@ -169,7 +169,7 @@ async fn handle_follow( let follow = generate_follow(state, &actor.id, &my_id)?; let inbox = actor.inbox().to_owned(); - db.add_listener(inbox); + db.add_listener(inbox).await?; let client2 = client.clone(); let inbox = actor.inbox().clone();