From 654178dd5c6213822690539ba7d2b412bc95431b Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 23 Apr 2021 22:23:24 -0400 Subject: [PATCH] Use static HTTP client instead --- src/cache/disk_cache.rs | 1 + src/routes.rs | 36 ++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/cache/disk_cache.rs b/src/cache/disk_cache.rs index e86aef2..1485093 100644 --- a/src/cache/disk_cache.rs +++ b/src/cache/disk_cache.rs @@ -175,6 +175,7 @@ impl Cache for DiskCache { &self, key: &CacheKey, ) -> Option> { + coz::scope!("disk cache get"); let channel = self.db_update_channel_sender.clone(); let path = Arc::new(self.disk_path.clone().join(PathBuf::from(key))); diff --git a/src/routes.rs b/src/routes.rs index 27a8a83..6d1724e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -12,6 +12,8 @@ use bytes::Bytes; use chrono::{DateTime, Utc}; use futures::{Stream, TryStreamExt}; use log::{debug, error, info, warn}; +use once_cell::sync::Lazy; +use reqwest::Client; use serde::Deserialize; use sodiumoxide::crypto::box_::{open_precomputed, Nonce, PrecomputedKey, NONCEBYTES}; use thiserror::Error; @@ -23,6 +25,8 @@ use crate::state::RwLockServerState; pub const BASE64_CONFIG: base64::Config = base64::Config::new(base64::CharacterSet::UrlSafe, false); +static HTTP_CLIENT: Lazy = Lazy::new(|| Client::new()); + const SERVER_ID_STRING: &str = concat!( env!("CARGO_CRATE_NAME"), " ", @@ -60,7 +64,7 @@ async fn token_data( return ServerResponse::TokenValidationError(e); } } - + coz::progress!(); fetch_image(state, cache, chapter_hash, file_name, false).await } @@ -89,7 +93,7 @@ pub async fn default(state: Data, req: HttpRequest) -> impl R req.path().chars().skip(1).collect::() ); 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, Err(e) => { error!("{}", e); @@ -207,19 +211,23 @@ async fn fetch_image( // holding the read lock until the await resolves. let resp = if is_data_saver { - reqwest::get(format!( - "{}/data-saver/{}/{}", - state.0.read().image_server, - &key.0, - &key.1 - )) + HTTP_CLIENT + .get(format!( + "{}/data-saver/{}/{}", + state.0.read().image_server, + &key.0, + &key.1 + )) + .send() } else { - reqwest::get(format!( - "{}/data/{}/{}", - state.0.read().image_server, - &key.0, - &key.1 - )) + HTTP_CLIENT + .get(format!( + "{}/data/{}/{}", + state.0.read().image_server, + &key.0, + &key.1 + )) + .send() } .await;