initial work into lowmem

feature/v32-tokens
Edward Shen 2021-04-17 23:19:27 -04:00
parent 6181486827
commit 63a2e0beb1
Signed by: edward
GPG Key ID: 19182661E818369F
5 changed files with 29 additions and 14 deletions

9
src/cache/fs.rs vendored
View File

@ -9,10 +9,10 @@ use std::time::Duration;
use bytes::{Bytes, BytesMut};
use futures::{Future, Stream, StreamExt};
use once_cell::sync::Lazy;
use parking_lot::RwLock;
use reqwest::Error;
use tokio::fs::{remove_file, File};
use tokio::io::{AsyncRead, AsyncWriteExt, ReadBuf};
use tokio::sync::RwLock;
use tokio::time::Sleep;
/// 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() {
let status = WRITING_STATUS
.read()
.await
.get(path)
.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
/// a stream that reads from disk instead.
pub async fn transparent_file_stream(
pub async fn write_file(
path: &Path,
mut byte_stream: impl Stream<Item = Result<Bytes, Error>> + Unpin + Send + 'static,
) -> Result<FromFsStream, std::io::Error> {
let done_writing_flag = Arc::new(CacheStatus::new());
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.
write_lock.insert(path.to_path_buf(), Arc::clone(&done_writing_flag));
file
@ -87,7 +88,7 @@ pub async fn transparent_file_stream(
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
// it's possible we have an inconsistent state
if errored {

View File

@ -37,8 +37,11 @@ impl Cache for LowMemCache {
}
}
async fn put_stream(&mut self, key: CacheKey, image: ByteStream) {
// this call has a side effect and the returned future is for reading
let _ = super::fs::transparent_file_stream(&PathBuf::from(key), image);
async fn put_stream(
&mut self,
key: CacheKey,
image: ByteStream,
) -> Result<FromFsStream, std::io::Error> {
super::fs::write_file(&PathBuf::from(key), image).await
}
}

6
src/cache/mod.rs vendored
View File

@ -147,7 +147,11 @@ pub trait Cache: Send + Sync {
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!()
}
}

View File

@ -37,4 +37,6 @@ pub struct CliArgs {
pub enable_server_string: bool,
#[clap(short, long, conflicts_with("memory-quota"), env = "LOW_MEMORY_MODE")]
pub low_memory: bool,
#[clap(short, long, parse(from_occurrences))]
pub verbose: usize,
}

View File

@ -1,6 +1,6 @@
#![warn(clippy::pedantic, clippy::nursery)]
// 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::process;
@ -53,7 +53,9 @@ async fn main() -> Result<(), std::io::Error> {
println!(concat!(
env!("CARGO_PKG_NAME"),
" Copyright (C) 2021 Edward Shen\n\n",
" Copyright (C) 2021 ",
env!("CARGO_PKG_AUTHORS"),
"\n\n",
env!("CARGO_PKG_NAME"),
" 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\
@ -76,10 +78,13 @@ async fn main() -> Result<(), std::io::Error> {
let cache_path = cli_args.cache_path.clone();
let low_mem_mode = cli_args.low_memory;
SimpleLogger::new()
.with_level(LevelFilter::Info)
.init()
.unwrap();
match cli_args.verbose {
0 => SimpleLogger::new().with_level(LevelFilter::Info),
1 => SimpleLogger::new().with_level(LevelFilter::Debug),
_ => SimpleLogger::new().with_level(LevelFilter::Trace),
}
.init()
.unwrap();
let client_secret = if let Ok(v) = env::var("CLIENT_SECRET") {
v