From 78f3a233ca24d4df123f2da35897f3e5eec50fc8 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 18 Apr 2022 04:17:35 -0700 Subject: refactor: constify many constants --- src/main.rs | 4 ++-- src/route.rs | 4 +++- src/search.rs | 8 ++++++-- 3 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index d529f5c..044002e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,7 @@ use tokio::time::Instant; use windmark::{Response, Router}; use yarte::Template; -const CACHE_RATE: u64 = 60 * 5; +const SEARCH_SIZE: usize = 10; static DATABASE: SyncLazy> = SyncLazy::new(|| { Mutex::new({ @@ -209,7 +209,7 @@ async fn main() -> Result<(), Box> { ) .parse_query(&query.0.to_string()) .unwrap(), - &tantivy::collector::TopDocs::with_limit(10), + &tantivy::collector::TopDocs::with_limit(SEARCH_SIZE), ) .unwrap(); diff --git a/src/route.rs b/src/route.rs index 7dfa5e7..7140e52 100644 --- a/src/route.rs +++ b/src/route.rs @@ -22,6 +22,8 @@ use tokio::time::Instant; use crate::ROUTES; +pub const CACHE_RATE: u64 = 60 * 5; + #[derive(Debug)] pub struct Route { pub description: String, @@ -62,7 +64,7 @@ pub fn cache(context: &windmark::returnable::RouteContext<'_>, response: &str) { SyncLazy::new(|| Mutex::new(Instant::now())); if (*LAST_CACHED.lock().unwrap()).elapsed() - >= std::time::Duration::from_secs(crate::CACHE_RATE) + >= std::time::Duration::from_secs(CACHE_RATE) || (*ROUTES.lock().unwrap()) .get(context.url.path()) .is_some_and(|&r| r.text_cache.is_empty()) diff --git a/src/search.rs b/src/search.rs index 5e5bc53..9631885 100644 --- a/src/search.rs +++ b/src/search.rs @@ -21,6 +21,8 @@ use std::{lazy::SyncLazy, sync::Mutex}; use tantivy::schema; use tempfile::TempDir; +const SEARCH_INDEX_SIZE: usize = 10_000_000; + pub static INDEX_PATH: SyncLazy> = SyncLazy::new(|| Mutex::new(TempDir::new().unwrap())); pub static SCHEMA: SyncLazy> = @@ -47,7 +49,7 @@ pub static INDEX: SyncLazy> = SyncLazy::new(|| { }); pub static INDEX_WRITER: SyncLazy> = SyncLazy::new(|| { - Mutex::new((*INDEX.lock().unwrap()).writer(10_000_000).unwrap()) + Mutex::new((*INDEX.lock().unwrap()).writer(SEARCH_INDEX_SIZE).unwrap()) }); pub fn index() { @@ -84,6 +86,8 @@ pub fn index() { time.elapsed().as_nanos() as f64 / 1_000_000.0 ); - std::thread::sleep(std::time::Duration::from_secs(1)); + std::thread::sleep(std::time::Duration::from_secs( + crate::route::CACHE_RATE, + )); } } -- cgit v1.2.3