From 6b9c879ba71a0c1af868b0de0e9e830414fe313a Mon Sep 17 00:00:00 2001 From: Katalina Okano Date: Fri, 9 Jun 2023 10:43:41 -0400 Subject: [PATCH] improve tracing, bundle sqlite --- Cargo.lock | 2 ++ Cargo.toml | 3 ++- src/handlers.rs | 15 +++++++++++++-- src/main.rs | 5 ++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f36f08..3906675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "r2d2", "r2d2_sqlite", "reqwest", + "rusqlite", "serde", "serde_json", "tokio", @@ -577,6 +578,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326" dependencies = [ + "cc", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index 299f4dc..c878b22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ bincode = "1.3" chrono = { version = "0.4", features = ["serde"] } tower-http = { version = "0.4", features = ["trace"] } r2d2_sqlite = "0.22" -r2d2 = "0.8" \ No newline at end of file +r2d2 = "0.8" +rusqlite = { version = "0.29.0", features = ["bundled"] } \ No newline at end of file diff --git a/src/handlers.rs b/src/handlers.rs index da72987..2d7ba0b 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -84,29 +84,40 @@ pub async fn get_world( response } +#[tracing::instrument(skip(db))] fn world_from_cache(db: r2d2::Pool, world: i32) -> Result { let db = db.get().unwrap(); let mut query = db.prepare("SELECT data FROM worlds WHERE id = ?").unwrap(); let value: Result, _> = query.query_row(params![world], |r| r.get(0)); if value.is_err() { + tracing::debug!("Cache miss (non-exist) for world {}", world); return Err(()); } match bincode::deserialize::(value.unwrap().as_slice()) { Ok(response) => { if response.cached_at + chrono::Duration::minutes(5) < chrono::Utc::now() { + tracing::debug!("Cache miss (expired) for world {}", world); return Err(()); } + + tracing::debug!("Cache hit for world {}", world); Ok(response) } - _ => Err(()), + _ => { + tracing::debug!("Cache miss (corrupt) for world {}", world); + Err(()) + } } } +#[tracing::instrument(skip(db, response))] fn world_to_cache(db: r2d2::Pool, world: i32, response: &Response) { let value = bincode::serialize(response).unwrap(); let db = db.get().unwrap(); - let mut query = db.prepare("INSERT INTO worlds (id, data) VALUES (?, ?) ON CONFLICT DO UPDATE SET data=excluded.data").unwrap(); + let mut query = db + .prepare("INSERT OR REPLACE INTO worlds (id, data) VALUES (?, ?)") + .unwrap(); query.execute(params![world, value]).unwrap(); } diff --git a/src/main.rs b/src/main.rs index 4418029..1c9498d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,10 @@ mod types; #[tokio::main] async fn main() { tracing_subscriber::fmt() - .with_env_filter("tower_http=trace") + .with_env_filter( + tracing_subscriber::EnvFilter::from_default_env() + .add_directive("tower_http=trace".parse().unwrap()), + ) .init(); let sqlite_manager = SqliteConnectionManager::memory();