Use static HTTP client instead

This commit is contained in:
Edward Shen 2021-04-23 22:23:24 -04:00
parent 424dc09ae3
commit 654178dd5c
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 23 additions and 14 deletions

View file

@ -175,6 +175,7 @@ impl Cache for DiskCache {
&self, &self,
key: &CacheKey, key: &CacheKey,
) -> Option<Result<(CacheStream, ImageMetadata), CacheError>> { ) -> Option<Result<(CacheStream, ImageMetadata), CacheError>> {
coz::scope!("disk cache get");
let channel = self.db_update_channel_sender.clone(); let channel = self.db_update_channel_sender.clone();
let path = Arc::new(self.disk_path.clone().join(PathBuf::from(key))); let path = Arc::new(self.disk_path.clone().join(PathBuf::from(key)));

View file

@ -12,6 +12,8 @@ use bytes::Bytes;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use futures::{Stream, TryStreamExt}; use futures::{Stream, TryStreamExt};
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use once_cell::sync::Lazy;
use reqwest::Client;
use serde::Deserialize; use serde::Deserialize;
use sodiumoxide::crypto::box_::{open_precomputed, Nonce, PrecomputedKey, NONCEBYTES}; use sodiumoxide::crypto::box_::{open_precomputed, Nonce, PrecomputedKey, NONCEBYTES};
use thiserror::Error; use thiserror::Error;
@ -23,6 +25,8 @@ use crate::state::RwLockServerState;
pub const BASE64_CONFIG: base64::Config = base64::Config::new(base64::CharacterSet::UrlSafe, false); pub const BASE64_CONFIG: base64::Config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| Client::new());
const SERVER_ID_STRING: &str = concat!( const SERVER_ID_STRING: &str = concat!(
env!("CARGO_CRATE_NAME"), env!("CARGO_CRATE_NAME"),
" ", " ",
@ -60,7 +64,7 @@ async fn token_data(
return ServerResponse::TokenValidationError(e); return ServerResponse::TokenValidationError(e);
} }
} }
coz::progress!();
fetch_image(state, cache, chapter_hash, file_name, false).await fetch_image(state, cache, chapter_hash, file_name, false).await
} }
@ -89,7 +93,7 @@ pub async fn default(state: Data<RwLockServerState>, req: HttpRequest) -> impl R
req.path().chars().skip(1).collect::<String>() req.path().chars().skip(1).collect::<String>()
); );
info!("Got unknown path, just proxying: {}", path); info!("Got unknown path, just proxying: {}", path);
let resp = match reqwest::get(path).await { let resp = match HTTP_CLIENT.get(path).send().await {
Ok(resp) => resp, Ok(resp) => resp,
Err(e) => { Err(e) => {
error!("{}", e); error!("{}", e);
@ -207,19 +211,23 @@ async fn fetch_image(
// holding the read lock until the await resolves. // holding the read lock until the await resolves.
let resp = if is_data_saver { let resp = if is_data_saver {
reqwest::get(format!( HTTP_CLIENT
.get(format!(
"{}/data-saver/{}/{}", "{}/data-saver/{}/{}",
state.0.read().image_server, state.0.read().image_server,
&key.0, &key.0,
&key.1 &key.1
)) ))
.send()
} else { } else {
reqwest::get(format!( HTTP_CLIENT
.get(format!(
"{}/data/{}/{}", "{}/data/{}/{}",
state.0.read().image_server, state.0.read().image_server,
&key.0, &key.0,
&key.1 &key.1
)) ))
.send()
} }
.await; .await;