refactor: make globals simple

This commit is contained in:
Jun Kurihara 2023-07-07 20:24:38 +09:00
commit 80e10d5ccd
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
4 changed files with 37 additions and 19 deletions

View file

@ -205,7 +205,7 @@ fn get_reverse_proxy(
let mut upstream: HashMap<PathNameBytesExp, UpstreamGroup> = HashMap::default(); let mut upstream: HashMap<PathNameBytesExp, UpstreamGroup> = HashMap::default();
rp_settings.iter().for_each(|rpo| { rp_settings.iter().for_each(|rpo| {
let upstream_vec: Vec<Upstream> = rpo.upstream.iter().map(|x| x.to_upstream().unwrap()).collect(); let upstream_vec: Vec<Upstream> = rpo.upstream.iter().map(|x| x.try_into().unwrap()).collect();
// let upstream_iter = rpo.upstream.iter().map(|x| x.to_upstream().unwrap()); // let upstream_iter = rpo.upstream.iter().map(|x| x.to_upstream().unwrap());
// let lb_upstream_num = vec_upstream.len(); // let lb_upstream_num = vec_upstream.len();
let elem = UpstreamGroupBuilder::default() let elem = UpstreamGroupBuilder::default()

View file

@ -65,8 +65,11 @@ pub struct UpstreamParams {
pub location: String, pub location: String,
pub tls: Option<bool>, pub tls: Option<bool>,
} }
impl UpstreamParams {
pub fn to_upstream(&self) -> Result<Upstream> { impl TryInto<Upstream> for &UpstreamParams {
type Error = RpxyError;
fn try_into(self) -> std::result::Result<Upstream, Self::Error> {
let mut scheme = "http"; let mut scheme = "http";
if let Some(t) = self.tls { if let Some(t) = self.tls {
if t { if t {
@ -81,9 +84,9 @@ impl UpstreamParams {
} }
impl ConfigToml { impl ConfigToml {
pub fn new(config_file: &str) -> std::result::Result<Self, anyhow::Error> { pub fn new(config_file: &str) -> std::result::Result<Self, RpxyError> {
let config_str = fs::read_to_string(config_file).context("Failed to read config file")?; 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)
} }
} }

View file

@ -29,6 +29,9 @@ pub enum RpxyError {
#[error("I/O Error")] #[error("I/O Error")]
Io(#[from] io::Error), Io(#[from] io::Error),
#[error("Toml Deserialization Error")]
TomlDe(#[from] toml::de::Error),
#[cfg(feature = "http3")] #[cfg(feature = "http3")]
#[error("Quic Connection Error")] #[error("Quic Connection Error")]
QuicConn(#[from] quinn::ConnectionError), QuicConn(#[from] quinn::ConnectionError),

View file

@ -6,25 +6,24 @@ use std::sync::{
}; };
use tokio::time::Duration; 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 struct Globals {
pub listen_sockets: Vec<SocketAddr>, pub listen_sockets: Vec<SocketAddr>, // when instantiate server
pub http_port: Option<u16>, pub http_port: Option<u16>, // when instantiate server
pub https_port: Option<u16>, pub https_port: Option<u16>, // when instantiate server
pub proxy_timeout: Duration, pub proxy_timeout: Duration, // when serving requests at Proxy
pub upstream_timeout: Duration, pub upstream_timeout: Duration, // when serving requests at Handler
pub max_clients: usize, pub max_clients: usize, // when serving requests
pub request_count: RequestCount, pub max_concurrent_streams: u32, // when instantiate server
pub max_concurrent_streams: u32, pub keepalive: bool, // when instantiate server
pub keepalive: bool,
pub runtime_handle: tokio::runtime::Handle,
pub backends: Backends,
// experimentals // experimentals
pub sni_consistency: bool, pub sni_consistency: bool, // Handler
// All need to make packet acceptor
#[cfg(feature = "http3")] #[cfg(feature = "http3")]
pub http3: bool, pub http3: bool,
#[cfg(feature = "http3")] #[cfg(feature = "http3")]
@ -39,9 +38,22 @@ pub struct Globals {
pub h3_max_concurrent_connections: u32, pub h3_max_concurrent_connections: u32,
#[cfg(feature = "http3")] #[cfg(feature = "http3")]
pub h3_max_idle_timeout: Option<quinn::IdleTimeout>, pub h3_max_idle_timeout: Option<quinn::IdleTimeout>,
// 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)] #[derive(Debug, Clone, Default)]
/// Counter for serving requests
pub struct RequestCount(Arc<AtomicUsize>); pub struct RequestCount(Arc<AtomicUsize>);
impl RequestCount { impl RequestCount {