Skip to content

Commit

Permalink
Preconnect RPC server to reduce cache miss latency
Browse files Browse the repository at this point in the history
  • Loading branch information
james58899 committed Feb 22, 2024
1 parent 474a756 commit f32f35b
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 39 deletions.
98 changes: 86 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ actix-tls = { version = "*", default-features = false, features = ["accept"] }
actix-web = { version = "4.5", default-features = false, features = ["macros", "openssl"] }
actix-web-lab = "0.20"
async-stream = "0.3"
bytes = "1.5"
chrono = "0.4"
clap = { version = "4.5", features = ["derive", "wrap_help"] }
cpufeatures = "0.2"
filesize = "0.2"
filetime = "0.2"
futures = "0.3"
hex = "0.4"
http-body-util = "0.1"
hyper = { version = "1.2", features = ["client", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] }
inquire = "0.6"
log = { version = "0.4", features = ["std"] }
mime = "0.3"
Expand All @@ -28,6 +32,7 @@ rand = { version = "0.8", default-features = false, features = ["small_rng"] }
regex = "1.10"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "stream", "socks"] }
scopeguard = "1.2"
socket2 = "0.5"
tempfile = "3.10"
tokio = { version = "1", features = ["full", "parking_lot"] }
tokio-stream = { version = "0.1", default-features = false, features = ["fs"] }
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use core::fmt;

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
pub enum Error {
VersionTooOld,
ApiResponseFail { fail_code: String, message: String },
ConnectTestFail,
InitSettingsMissing(String),
HashMismatch { expected: [u8; 20], actual: [u8; 20] },
ServerError { status: u16, body: Option<String> },
}

impl Error {
Expand All @@ -28,6 +30,7 @@ impl fmt::Display for Error {
Error::ConnectTestFail => write!(f, "Connect test failed"),
Error::InitSettingsMissing(settings) => write!(f, "Missing init settings: {settings}"),
Error::HashMismatch { expected, actual } => write!(f, "Hash missmatch. Expected={expected:?}, Actual={actual:?}"),
Error::ServerError { status, body } => write!(f, "Status={status}, Body={}", body.clone().unwrap_or_default()),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mod logger;
mod middleware;
mod route;
mod rpc;
mod rpc_http_client;
mod util;

#[cfg(not(target_env = "msvc"))]
Expand Down
42 changes: 15 additions & 27 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use rand::prelude::SliceRandom;
use reqwest::{IntoUrl, Url};

use crate::{
error::Error,
gallery_downloader::GalleryMeta,
util::{create_http_client, string_to_hash},
};
use crate::{error::Error, gallery_downloader::GalleryMeta, rpc_http_client::RPCHttpClient, util::string_to_hash};

const API_VERSION: i32 = 160; // For server check capabilities.
const DEFAULT_SERVER: &str = "rpc.hentaiathome.net";
Expand All @@ -39,7 +35,7 @@ pub struct RPCClient {
clock_offset: AtomicI64,
id: i32,
key: String,
reqwest: reqwest::Client,
http_client: RPCHttpClient,
rpc_servers: RwLock<Vec<String>>,
running: AtomicBool,
settings: Arc<Settings>,
Expand Down Expand Up @@ -128,7 +124,7 @@ impl RPCClient {
clock_offset: AtomicI64::new(0),
id,
key: key.to_string(),
reqwest: create_http_client(Duration::from_secs(600), None),
http_client: RPCHttpClient::new(Duration::from_secs(600)),
rpc_servers: RwLock::new(vec![]),
running: AtomicBool::new(false),
settings: Arc::new(Settings {
Expand Down Expand Up @@ -206,11 +202,9 @@ impl RPCClient {
pub async fn get_cert(&self) -> Option<ParsedPkcs12_2> {
let _provider = Provider::try_load(None, "legacy", true).unwrap();
let cert = self
.reqwest
.http_client
.get(self.build_url("get_cert", "", None))
.send()
.and_then(|res| async { res.error_for_status() })
.and_then(|res| res.bytes())
.and_then(|res| self.http_client.to_bytes(res))
.await
.ok()
.and_then(|data| Pkcs12::from_der(&data[..]).ok())
Expand Down Expand Up @@ -474,10 +468,9 @@ The program will now terminate.
return Ok(response);
}
Err(err) => {
if err.is_connect() || err.is_timeout() || err.status().map_or(false, |s| s.is_server_error()) {
self.change_server();
}
error = Box::new(err);
error!("Send request error: {}", err);
self.change_server();
error = err;
}
}
retry -= 1;
Expand All @@ -486,19 +479,14 @@ The program will now terminate.
Err(error)
}

async fn send_request<U: IntoUrl>(&self, url: U) -> Result<String, reqwest::Error> {
let res = self.reqwest.get(url).timeout(Duration::from_secs(600)).send().await?;

if let Err(err) = res.error_for_status_ref() {
warn!(
"Server response error: code={}, body={}",
res.status(),
res.text().await.unwrap_or_default()
);
return Err(err);
async fn send_request<U: IntoUrl>(&self, url: U) -> Result<String, RequestError> {
match self.http_client.get(url).await {
Ok(res) => self.http_client.to_text(res).await,
Err(err) => {
warn!("Server response error: {}", err);
Err(err)
}
}

res.text().await
}

fn build_url(&self, action: &str, additional: &str, endpoint: Option<&str>) -> Url {
Expand Down
Loading

0 comments on commit f32f35b

Please sign in to comment.