initial work into lowmem
This commit is contained in:
parent
6181486827
commit
63a2e0beb1
5 changed files with 29 additions and 14 deletions
9
src/cache/fs.rs
vendored
9
src/cache/fs.rs
vendored
|
@ -9,10 +9,10 @@ use std::time::Duration;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{Future, Stream, StreamExt};
|
use futures::{Future, Stream, StreamExt};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use parking_lot::RwLock;
|
|
||||||
use reqwest::Error;
|
use reqwest::Error;
|
||||||
use tokio::fs::{remove_file, File};
|
use tokio::fs::{remove_file, File};
|
||||||
use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf};
|
use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
use tokio::time::Sleep;
|
use tokio::time::Sleep;
|
||||||
|
|
||||||
/// Keeps track of files that are currently being written to.
|
/// Keeps track of files that are currently being written to.
|
||||||
|
@ -39,6 +39,7 @@ pub async fn read_file(path: &Path) -> Option<Result<FromFsStream, std::io::Erro
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
let status = WRITING_STATUS
|
let status = WRITING_STATUS
|
||||||
.read()
|
.read()
|
||||||
|
.await
|
||||||
.get(path)
|
.get(path)
|
||||||
.map_or_else(|| Arc::new(CacheStatus::done()), Arc::clone);
|
.map_or_else(|| Arc::new(CacheStatus::done()), Arc::clone);
|
||||||
|
|
||||||
|
@ -50,14 +51,14 @@ pub async fn read_file(path: &Path) -> Option<Result<FromFsStream, std::io::Erro
|
||||||
|
|
||||||
/// Maps the input byte stream into one that writes to disk instead, returning
|
/// Maps the input byte stream into one that writes to disk instead, returning
|
||||||
/// a stream that reads from disk instead.
|
/// a stream that reads from disk instead.
|
||||||
pub async fn transparent_file_stream(
|
pub async fn write_file(
|
||||||
path: &Path,
|
path: &Path,
|
||||||
mut byte_stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static,
|
mut byte_stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static,
|
||||||
) -> Result<FromFsStream, std::io::Error> {
|
) -> Result<FromFsStream, std::io::Error> {
|
||||||
let done_writing_flag = Arc::new(CacheStatus::new());
|
let done_writing_flag = Arc::new(CacheStatus::new());
|
||||||
|
|
||||||
let mut file = {
|
let mut file = {
|
||||||
let mut write_lock = WRITING_STATUS.write();
|
let mut write_lock = WRITING_STATUS.write().await;
|
||||||
let file = File::create(path).await?; // we need to make sure the file exists and is truncated.
|
let file = File::create(path).await?; // we need to make sure the file exists and is truncated.
|
||||||
write_lock.insert(path.to_path_buf(), Arc::clone(&done_writing_flag));
|
write_lock.insert(path.to_path_buf(), Arc::clone(&done_writing_flag));
|
||||||
file
|
file
|
||||||
|
@ -87,7 +88,7 @@ pub async fn transparent_file_stream(
|
||||||
file.sync_all().await?; // we need metadata
|
file.sync_all().await?; // we need metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut write_lock = WRITING_STATUS.write();
|
let mut write_lock = WRITING_STATUS.write().await;
|
||||||
// This needs to be written atomically with the write lock, else
|
// This needs to be written atomically with the write lock, else
|
||||||
// it's possible we have an inconsistent state
|
// it's possible we have an inconsistent state
|
||||||
if errored {
|
if errored {
|
||||||
|
|
9
src/cache/low_mem.rs
vendored
9
src/cache/low_mem.rs
vendored
|
@ -37,8 +37,11 @@ impl Cache for LowMemCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn put_stream(&mut self, key: CacheKey, image: ByteStream) {
|
async fn put_stream(
|
||||||
// this call has a side effect and the returned future is for reading
|
&mut self,
|
||||||
let _ = super::fs::transparent_file_stream(&PathBuf::from(key), image);
|
key: CacheKey,
|
||||||
|
image: ByteStream,
|
||||||
|
) -> Result<FromFsStream, std::io::Error> {
|
||||||
|
super::fs::write_file(&PathBuf::from(key), image).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
src/cache/mod.rs
vendored
6
src/cache/mod.rs
vendored
|
@ -147,7 +147,11 @@ pub trait Cache: Send + Sync {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn put_stream(&mut self, _key: CacheKey, _image: ByteStream) {
|
async fn put_stream(
|
||||||
|
&mut self,
|
||||||
|
_key: CacheKey,
|
||||||
|
_image: ByteStream,
|
||||||
|
) -> Result<FromFsStream, std::io::Error> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,4 +37,6 @@ pub struct CliArgs {
|
||||||
pub enable_server_string: bool,
|
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")]
|
||||||
pub low_memory: bool,
|
pub low_memory: bool,
|
||||||
|
#[clap(short, long, parse(from_occurrences))]
|
||||||
|
pub verbose: usize,
|
||||||
}
|
}
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
#![warn(clippy::pedantic, clippy::nursery)]
|
#![warn(clippy::pedantic, clippy::nursery)]
|
||||||
// We're end users, so these is ok
|
// We're end users, so these is ok
|
||||||
#![allow(clippy::future_not_send, clippy::module_name_repetitions)]
|
#![allow(clippy::module_name_repetitions)]
|
||||||
|
|
||||||
use std::env::{self, VarError};
|
use std::env::{self, VarError};
|
||||||
use std::process;
|
use std::process;
|
||||||
|
@ -53,7 +53,9 @@ async fn main() -> Result<(), std::io::Error> {
|
||||||
|
|
||||||
println!(concat!(
|
println!(concat!(
|
||||||
env!("CARGO_PKG_NAME"),
|
env!("CARGO_PKG_NAME"),
|
||||||
" Copyright (C) 2021 Edward Shen\n\n",
|
" Copyright (C) 2021 ",
|
||||||
|
env!("CARGO_PKG_AUTHORS"),
|
||||||
|
"\n\n",
|
||||||
env!("CARGO_PKG_NAME"),
|
env!("CARGO_PKG_NAME"),
|
||||||
" is free software: you can redistribute it and/or modify\n\
|
" is free software: you can redistribute it and/or modify\n\
|
||||||
it under the terms of the GNU General Public License as published by\n\
|
it under the terms of the GNU General Public License as published by\n\
|
||||||
|
@ -76,10 +78,13 @@ async fn main() -> Result<(), std::io::Error> {
|
||||||
let cache_path = cli_args.cache_path.clone();
|
let cache_path = cli_args.cache_path.clone();
|
||||||
let low_mem_mode = cli_args.low_memory;
|
let low_mem_mode = cli_args.low_memory;
|
||||||
|
|
||||||
SimpleLogger::new()
|
match cli_args.verbose {
|
||||||
.with_level(LevelFilter::Info)
|
0 => SimpleLogger::new().with_level(LevelFilter::Info),
|
||||||
.init()
|
1 => SimpleLogger::new().with_level(LevelFilter::Debug),
|
||||||
.unwrap();
|
_ => SimpleLogger::new().with_level(LevelFilter::Trace),
|
||||||
|
}
|
||||||
|
.init()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let client_secret = if let Ok(v) = env::var("CLIENT_SECRET") {
|
let client_secret = if let Ok(v) = env::var("CLIENT_SECRET") {
|
||||||
v
|
v
|
||||||
|
|
Loading…
Reference in a new issue