add pruning

feature/v32-tokens
Edward Shen 2021-04-18 17:38:33 -04:00
parent 525aef91bf
commit ae216b2410
Signed by: edward
GPG Key ID: 19182661E818369F
4 changed files with 40 additions and 14 deletions

View File

@ -3,6 +3,7 @@
use std::path::PathBuf;
use async_trait::async_trait;
use log::warn;
use lru::LruCache;
use super::{BoxedImageStream, Cache, CacheError, CacheKey, CacheStream, ImageMetadata};
@ -25,8 +26,6 @@ impl LowMemCache {
}
}
// todo: schedule eviction
#[async_trait]
impl Cache for LowMemCache {
async fn get(
@ -55,4 +54,8 @@ impl Cache for LowMemCache {
.map(move |stream| (stream, self.on_disk.get(&key).unwrap()))
.map_err(Into::into)
}
async fn prune(&mut self) {
warn!("Trimming has not been implemented yet. Cache is unbounded!");
}
}

6
src/cache/mod.rs vendored
View File

@ -10,6 +10,7 @@ use bytes::Bytes;
use chrono::{DateTime, FixedOffset};
use fs::FsStream;
use futures::{Stream, StreamExt};
use log::debug;
use thiserror::Error;
pub use fs::UpstreamError;
@ -148,12 +149,17 @@ pub trait Cache: Send + Sync {
&mut self,
key: &CacheKey,
) -> Option<Result<(CacheStream, &ImageMetadata), CacheError>>;
async fn put(
&mut self,
key: CacheKey,
image: BoxedImageStream,
metadata: ImageMetadata,
) -> Result<(CacheStream, &ImageMetadata), CacheError>;
async fn prune(&mut self) {
debug!("Would trim but cache does not implement trimming!");
}
}
pub enum CacheStream {

View File

@ -35,7 +35,13 @@ pub struct CliArgs {
/// reasons.
#[clap(long, env = "ENABLE_SERVER_STRING", takes_value = false)]
pub enable_server_string: bool,
#[clap(short, long, conflicts_with("memory-quota"), env = "LOW_MEMORY_MODE")]
#[clap(
short,
long,
conflicts_with("memory-quota"),
env = "LOW_MEMORY_MODE",
takes_value = false
)]
pub low_memory: bool,
#[clap(short, long, parse(from_occurrences))]
pub verbose: usize,

View File

@ -130,24 +130,35 @@ async fn main() -> Result<(), std::io::Error> {
}
});
let cache: Box<dyn Cache> = if low_mem_mode {
Box::new(LowMemCache::new(disk_quota, cache_path.clone()))
} else {
Box::new(GenerationalCache::new(
memory_max_size,
disk_quota,
cache_path.clone(),
))
};
let cache = Arc::new(Mutex::new(cache));
let cache1 = Arc::clone(&cache);
// Spawn periodic cache trimming
spawn(async move {
let mut interval = time::interval(Duration::from_secs(3 * 60));
loop {
interval.tick().await;
cache.lock().prune().await;
}
});
// Start HTTPS server
HttpServer::new(move || {
let cache: Box<dyn Cache> = if low_mem_mode {
Box::new(LowMemCache::new(disk_quota, cache_path.clone()))
} else {
Box::new(GenerationalCache::new(
memory_max_size,
disk_quota,
cache_path.clone(),
))
};
App::new()
.service(routes::token_data)
.service(routes::token_data_saver)
.route("{tail:.*}", web::get().to(routes::default))
.app_data(Data::from(Arc::clone(&data_1)))
.app_data(Data::new(Mutex::new(cache)))
.app_data(Data::from(Arc::clone(&cache1)))
})
.shutdown_timeout(60)
.bind_rustls(format!("0.0.0.0:{}", port), tls_config)?