add pruning
This commit is contained in:
parent
525aef91bf
commit
ae216b2410
4 changed files with 40 additions and 14 deletions
7
src/cache/low_mem.rs
vendored
7
src/cache/low_mem.rs
vendored
|
@ -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
6
src/cache/mod.rs
vendored
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -130,8 +130,6 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
}
|
||||
});
|
||||
|
||||
// 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 {
|
||||
|
@ -141,13 +139,26 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
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 || {
|
||||
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)?
|
||||
|
|
Loading…
Reference in a new issue