From 80e10d5ccd5b006e7c4b5a05326e7f710126d0c3 Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Fri, 7 Jul 2023 20:24:38 +0900 Subject: [PATCH] refactor: make globals simple --- src/config/parse.rs | 2 +- src/config/toml.rs | 13 ++++++++----- src/error.rs | 3 +++ src/globals.rs | 38 +++++++++++++++++++++++++------------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/config/parse.rs b/src/config/parse.rs index 8e4ddf7..9c4fd86 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -205,7 +205,7 @@ fn get_reverse_proxy( let mut upstream: HashMap = HashMap::default(); rp_settings.iter().for_each(|rpo| { - let upstream_vec: Vec = rpo.upstream.iter().map(|x| x.to_upstream().unwrap()).collect(); + let upstream_vec: Vec = rpo.upstream.iter().map(|x| x.try_into().unwrap()).collect(); // let upstream_iter = rpo.upstream.iter().map(|x| x.to_upstream().unwrap()); // let lb_upstream_num = vec_upstream.len(); let elem = UpstreamGroupBuilder::default() diff --git a/src/config/toml.rs b/src/config/toml.rs index 6ce48b2..29e76cc 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -65,8 +65,11 @@ pub struct UpstreamParams { pub location: String, pub tls: Option, } -impl UpstreamParams { - pub fn to_upstream(&self) -> Result { + +impl TryInto for &UpstreamParams { + type Error = RpxyError; + + fn try_into(self) -> std::result::Result { let mut scheme = "http"; if let Some(t) = self.tls { if t { @@ -81,9 +84,9 @@ impl UpstreamParams { } impl ConfigToml { - pub fn new(config_file: &str) -> std::result::Result { - let config_str = fs::read_to_string(config_file).context("Failed to read config file")?; + pub fn new(config_file: &str) -> std::result::Result { + let config_str = fs::read_to_string(config_file).map_err(RpxyError::Io)?; - toml::from_str(&config_str).context("Failed to parse toml config") + toml::from_str(&config_str).map_err(RpxyError::TomlDe) } } diff --git a/src/error.rs b/src/error.rs index 3fb3474..18b4307 100644 --- a/src/error.rs +++ b/src/error.rs @@ -29,6 +29,9 @@ pub enum RpxyError { #[error("I/O Error")] Io(#[from] io::Error), + #[error("Toml Deserialization Error")] + TomlDe(#[from] toml::de::Error), + #[cfg(feature = "http3")] #[error("Quic Connection Error")] QuicConn(#[from] quinn::ConnectionError), diff --git a/src/globals.rs b/src/globals.rs index 0bd06a6..579695f 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -6,25 +6,24 @@ use std::sync::{ }; use tokio::time::Duration; +/// Global object containing proxy configurations and shared object like counters. +/// But note that in Globals, we do not have Mutex and RwLock. It is indeed, the context shared among async tasks. pub struct Globals { - pub listen_sockets: Vec, - pub http_port: Option, - pub https_port: Option, + pub listen_sockets: Vec, // when instantiate server + pub http_port: Option, // when instantiate server + pub https_port: Option, // when instantiate server - pub proxy_timeout: Duration, - pub upstream_timeout: Duration, + pub proxy_timeout: Duration, // when serving requests at Proxy + pub upstream_timeout: Duration, // when serving requests at Handler - pub max_clients: usize, - pub request_count: RequestCount, - pub max_concurrent_streams: u32, - pub keepalive: bool, - - pub runtime_handle: tokio::runtime::Handle, - pub backends: Backends, + pub max_clients: usize, // when serving requests + pub max_concurrent_streams: u32, // when instantiate server + pub keepalive: bool, // when instantiate server // experimentals - pub sni_consistency: bool, + pub sni_consistency: bool, // Handler + // All need to make packet acceptor #[cfg(feature = "http3")] pub http3: bool, #[cfg(feature = "http3")] @@ -39,9 +38,22 @@ pub struct Globals { pub h3_max_concurrent_connections: u32, #[cfg(feature = "http3")] pub h3_max_idle_timeout: Option, + + // Shared context + // Backend application objects to which http request handler forward incoming requests + pub backends: Backends, + // Counter for serving requests + pub request_count: RequestCount, + // Async task runtime handler + pub runtime_handle: tokio::runtime::Handle, } +// // TODO: Implement default for default values +// #[derive(Debug, Clone)] +// pub struct ProxyConfig {} + #[derive(Debug, Clone, Default)] +/// Counter for serving requests pub struct RequestCount(Arc); impl RequestCount {