From 1b1a7b12ba156c1a331433e40c8d3654de02e174 Mon Sep 17 00:00:00 2001 From: qpismont Date: Fri, 26 Jan 2024 21:35:33 +0100 Subject: [PATCH] first iteration of memory/disk/mixed cleaning tick --- src/config.rs | 6 +++--- src/main.rs | 19 +++++++++++-------- src/storages/disk.rs | 16 +++++++++++++++- src/storages/memory.rs | 20 ++++++++++++++------ src/storages/mixed.rs | 5 ++++- src/storages/mod.rs | 10 ++++++---- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/config.rs b/src/config.rs index 164949d..52745ca 100644 --- a/src/config.rs +++ b/src/config.rs @@ -6,20 +6,20 @@ use serde::Deserialize; #[derive(Deserialize, Clone)] pub struct MemoryStorageConfig { pub max_size: usize, - pub ttl: usize, + pub ttl: u64, } #[derive(Deserialize, Clone)] pub struct DiskStorageConfig { pub path: PathBuf, - pub ttl: usize, + pub ttl: u64, pub max_size: usize, } #[derive(Deserialize, Clone)] pub struct MixedStorageConfig { pub path: PathBuf, - pub ttl: usize, + pub ttl: u64, pub max_size: usize, } diff --git a/src/main.rs b/src/main.rs index 9aee005..1dbdc80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,14 +44,17 @@ async fn main() -> anyhow::Result<()> { async fn launch_storages_tick(app_state: Arc>) { loop { - let mut app_state_locked = app_state.lock().await; - match app_state_locked.storage_pool.tick().await { - Ok(_) => todo!(), - Err(err) => println!("{}", err), - } - - std::mem::drop(app_state_locked); - tokio::time::sleep(std::time::Duration::from_secs(1)).await; + + let mut app_state_locked = app_state.lock().await; + let results = app_state_locked.storage_pool.tick().await; + let errs = results + .into_iter() + .filter(anyhow::Result::is_err) + .map(anyhow::Result::unwrap_err); + + for err in errs { + println!("error while tick {}", err); + } } } diff --git a/src/storages/disk.rs b/src/storages/disk.rs index 3f962a6..4994ccb 100644 --- a/src/storages/disk.rs +++ b/src/storages/disk.rs @@ -72,7 +72,21 @@ impl Storage for DiskStorage { Ok(()) } - async fn tick(&self) -> anyhow::Result<()> { + async fn tick(&mut self) -> anyhow::Result<()> { + let paths_deleted = self + .items + .iter() + .filter(|(_, item)| item.created_at.elapsed().unwrap().as_secs() >= self.config.ttl) + .map(|(_, item)| item.path.clone()) + .collect::>(); + + self.items + .retain(|_, item| item.created_at.elapsed().unwrap().as_secs() < self.config.ttl); + + for path in paths_deleted { + tokio::fs::remove_file(path).await?; + } + Ok(()) } diff --git a/src/storages/memory.rs b/src/storages/memory.rs index 35364a6..493867e 100644 --- a/src/storages/memory.rs +++ b/src/storages/memory.rs @@ -1,4 +1,8 @@ -use std::{collections::HashMap, pin::Pin, time::SystemTime}; +use std::{ + collections::HashMap, + pin::Pin, + time::{Duration, SystemTime}, +}; use anyhow::Ok; use async_trait::async_trait; @@ -43,7 +47,10 @@ impl Storage for MemoryStorage { Ok(()) } - async fn tick(&self) -> anyhow::Result<()> { + async fn tick(&mut self) -> anyhow::Result<()> { + self.items + .retain(|_, item| item.created_at.elapsed().unwrap().as_secs() < self.config.ttl); + Ok(()) } @@ -52,10 +59,11 @@ impl Storage for MemoryStorage { } async fn delete(&mut self, key: String) -> anyhow::Result<()> { - self.items - .remove(&key) - .ok_or_else(|| anyhow::anyhow!("")) - .map(|_| ()) + if self.items.get(&key).is_some() { + self.items.remove(&key); + } + + Ok(()) } async fn retrieve( diff --git a/src/storages/mixed.rs b/src/storages/mixed.rs index e824b2d..70454aa 100644 --- a/src/storages/mixed.rs +++ b/src/storages/mixed.rs @@ -36,7 +36,10 @@ impl Storage for MixedStorage { Ok(()) } - async fn tick(&self) -> anyhow::Result<()> { + async fn tick(&mut self) -> anyhow::Result<()> { + self.disk.tick().await?; + self.memory.tick().await?; + Ok(()) } diff --git a/src/storages/mod.rs b/src/storages/mod.rs index 705b953..1e8f008 100644 --- a/src/storages/mod.rs +++ b/src/storages/mod.rs @@ -19,7 +19,7 @@ use self::{disk::DiskStorage, memory::MemoryStorage, mixed::MixedStorage}; pub trait Storage { async fn init(&mut self) -> anyhow::Result<()>; async fn eligible(&self, src: String) -> bool; - async fn tick(&self) -> anyhow::Result<()>; + async fn tick(&mut self) -> anyhow::Result<()>; async fn delete(&mut self, key: String) -> anyhow::Result<()>; async fn retrieve( &self, @@ -51,12 +51,14 @@ impl StoragePool { Ok(()) } - pub async fn tick(&mut self) -> anyhow::Result<()> { + pub async fn tick(&mut self) -> Vec> { + let mut results = vec![]; + for item in &mut self.storages { - item.tick().await?; + results.push(item.tick().await); } - Ok(()) + results } pub async fn retrieve(