diff --git a/etc/main.yaml b/etc/main.yaml index 45434ee..7074835 100644 --- a/etc/main.yaml +++ b/etc/main.yaml @@ -1,15 +1,17 @@ version: 1 threads: 8 +#idle_timeout: 1000 upstream_keepalive_pool_size: 100 pid_file: /tmp/load_balancer.pid error_log: /tmp/load_balancer_err.log upgrade_sock: /tmp/load_balancer.sock -proxy_address_http: 0.0.0.0:6193 -proxy_address_tls: 0.0.0.0:6194 # Optionnal -tls_certificate: etc/server.crt # Mandatory if proxy_address_tls if exists -tls_key_file: etc/key.pem # Mandatory if proxy_address_tls if exists config_address: 0.0.0.0:3000 +proxy_address_http: 0.0.0.0:6193 +proxy_address_tls: 0.0.0.0:6194 # Optional +tls_certificate: etc/server.crt # Mandatory if proxy_address_tls is set +tls_key_file: etc/key.pem # Mandatory if proxy_address_tls is set upstreams_conf: etc/upstreams.yaml -#idle_timeout: 1000 log_level: info # info, warn, error, debug, trace, off +hc_method: HEAD +hc_interval: 2 diff --git a/etc/upstreams.yaml b/etc/upstreams.yaml index 33c6535..0610b9b 100644 --- a/etc/upstreams.yaml +++ b/etc/upstreams.yaml @@ -84,4 +84,10 @@ upstreams: "/": ssl: false servers: - - "192.168.1.5:8080" \ No newline at end of file + - "192.168.1.5:8080" + 127.0.0.2: + paths: + "/": + ssl: false + servers: + - "10.0.55.171:3000" \ No newline at end of file diff --git a/src/utils/healthcheck.rs b/src/utils/healthcheck.rs index 3160918..074766b 100644 --- a/src/utils/healthcheck.rs +++ b/src/utils/healthcheck.rs @@ -1,13 +1,13 @@ use crate::utils::tools::*; use dashmap::DashMap; -use log::warn; +use log::{error, warn}; use std::sync::atomic::AtomicUsize; use std::sync::Arc; use std::time::Duration; use tokio::time::interval; -pub async fn hc2(upslist: Arc, fullist: Arc) { - let mut period = interval(Duration::from_secs(2)); +pub async fn hc2(upslist: Arc, fullist: Arc, params: (&str, u64)) { + let mut period = interval(Duration::from_secs(params.1)); loop { tokio::select! { _ = period.tick() => { @@ -28,7 +28,7 @@ pub async fn hc2(upslist: Arc, fullist: Arc) false => _pref = "http://", } let link = format!("{}{}:{}{}", _pref, ip, port, path); - let resp = http_request(link.as_str(), "HEAD", "").await; + let resp = http_request(link.as_str(), params.0, "").await; match resp { true => { innervec.push(k.1.clone()); @@ -53,7 +53,7 @@ pub async fn hc2(upslist: Arc, fullist: Arc) #[allow(dead_code)] async fn http_request(url: &str, method: &str, payload: &str) -> bool { - let client = reqwest::Client::new(); + let client = reqwest::Client::builder().danger_accept_invalid_certs(true).build().unwrap(); let to = Duration::from_secs(1); match method { "POST" => { @@ -83,6 +83,9 @@ async fn http_request(url: &str, method: &str, payload: &str) -> bool { Err(_) => false, } } - _ => false, + _ => { + error!("Method {} not supported. Only GET|POST|HEAD are supported", method); + false + } } } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index b3e9db0..1db6a2c 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -65,7 +65,8 @@ impl BackgroundService for LB { let uu = self.ump_upst.clone(); let ff = self.ump_full.clone(); - let _ = tokio::spawn(async move { healthcheck::hc2(uu, ff).await }); + let (hc_method, hc_interval) = (self.config.get("hc_method").unwrap().clone(), self.config.get("hc_interval").unwrap().clone()); + let _ = tokio::spawn(async move { healthcheck::hc2(uu, ff, (&*hc_method.to_string(), hc_interval.to_string().parse().unwrap())).await }); loop { tokio::select! { @@ -301,7 +302,6 @@ impl ProxyHttp for LB { async fn logging(&self, session: &mut Session, _e: Option<&pingora::Error>, ctx: &mut Self::CTX) { let response_code = session.response_written().map_or(0, |resp| resp.status.as_u16()); debug!("{}, response code: {response_code}", self.request_summary(session, ctx)); - // info!("{}, response code: {response_code}", self.request_summary(session, ctx)); } }