From 548d64899ba296f52ae7c9982dd4948a89bd4b5a Mon Sep 17 00:00:00 2001 From: afterlifepro Date: Sun, 11 Jan 2026 11:56:16 +0000 Subject: [PATCH] use Arc> so the db connection can be shared across threads. needed for ingestion --- src/backfill/mod.rs | 10 ++++++---- src/db.rs | 6 ++++-- src/main.rs | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/backfill/mod.rs b/src/backfill/mod.rs index 1dc0cd1..ba8d7dd 100644 --- a/src/backfill/mod.rs +++ b/src/backfill/mod.rs @@ -6,6 +6,8 @@ //! 4. convert cbor data to json //! 5. store in db (limit to DB_MAX_REQ / 4 to avoid err) +use std::sync::Arc; + use ipld_core::cid::multibase::Base; use jacquard::url::Url; use sqlx::{Pool, Postgres, query}; @@ -34,7 +36,7 @@ pub enum Error { } pub async fn backfill( - conn: &Pool, + conn: Arc>, time: Option, ) -> Result<(), Error> { let car = load_car( @@ -51,7 +53,7 @@ pub async fn backfill( // erase all old records and return if it fails // we dont use diffs bc theyre complex and the overhead is minimal rn // only real overhead is network latency which would be ~= anyway - let _ = query!("DELETE FROM records").execute(conn).await?; + let _ = query!("DELETE FROM records").execute(&*conn).await?; let data = parse_car(&car) .await? @@ -85,11 +87,11 @@ pub async fn backfill( }, ); - if let Err(err) = query.build().execute(conn).await { + if let Err(err) = query.build().execute(&*conn).await { // couldnt backfill so go nuclear // this is program startup so its prolly safe lol println!("Got error \"{}\"\nDeleting records and exiting...", err); - let _ = query!("DELETE FROM records").execute(conn).await?; + let _ = query!("DELETE FROM records").execute(&*conn).await?; panic!() }; } diff --git a/src/db.rs b/src/db.rs index 6d09545..1a513e0 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,9 +1,11 @@ //! create a connection pool and setup tables before making avaliable +use std::sync::Arc; + use crate::config; use sqlx::{Pool, Postgres, migrate, postgres::PgPool}; -pub async fn conn() -> Pool { +pub async fn conn() -> Arc> { let conn = match PgPool::connect(&config::DATABASE_URL).await { Ok(val) => val, Err(err) => { @@ -15,5 +17,5 @@ pub async fn conn() -> Pool { panic!("Error running migrations: {}", err); }); - conn + Arc::new(conn) } diff --git a/src/main.rs b/src/main.rs index 8de8f4f..204f1f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ async fn main() -> Result<(), Error> { println!("Starting backfill"); let timer = std::time::Instant::now(); - backfill(&conn, Some(timer)) + backfill(conn.clone(), Some(timer)) .await .unwrap_or_else(|err| panic!("{}", err)); -- 2.51.2