From 5da70255a55c3d1094761624be6ff446d2d33eea Mon Sep 17 00:00:00 2001 From: Ara Sadoyan Date: Sat, 8 Feb 2025 15:05:33 +0100 Subject: [PATCH] initial commit --- src/main.rs | 2 +- src/web/proxyhttp.rs | 13 ++++++------- src/web/start.rs | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3603ffa..811df84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod web; fn main() { - web::start::run() + web::start::run(); } diff --git a/src/web/proxyhttp.rs b/src/web/proxyhttp.rs index 5703de4..ad0e82b 100644 --- a/src/web/proxyhttp.rs +++ b/src/web/proxyhttp.rs @@ -7,13 +7,11 @@ use pingora_http::{RequestHeader, ResponseHeader}; use pingora_proxy::{ProxyHttp, Session}; use std::any::type_name; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; #[allow(dead_code)] -pub fn typeoff(_: T) -> &'static str { +pub fn typeoff(_: T) { let to = type_name::(); println!("{:?}", to); - to } // pub struct LB(pub Arc>); @@ -28,14 +26,15 @@ pub struct LB { pub upstreams_map: DashMap, AtomicUsize)>, } +#[async_trait] pub trait GetHost { - fn get_host(&self, peer: &str) -> Option<(String, u16)>; + async fn get_host(&self, peer: &str) -> Option<(String, u16)>; fn set_host(&mut self, peer: &str, host: &str, port: u16); fn discover_hosts(&mut self); } - +#[async_trait] impl GetHost for LB { - fn get_host(&self, peer: &str) -> Option<(String, u16)> { + async fn get_host(&self, peer: &str) -> Option<(String, u16)> { // println!("{:?}", self.upstreams_map); // let entry = self.upstreams_map.get(peer)?; // let first = entry.value().first()?; @@ -114,7 +113,7 @@ impl ProxyHttp for LB { async fn upstream_peer(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result> { let host_name = session.req_header().headers.get("host"); let ddr = self.get_host(host_name.unwrap().to_str().unwrap()); - match ddr { + match ddr.await { Some((host, port)) => { let peer = Box::new(HttpPeer::new((host, port), false, "".to_string())); Ok(peer) diff --git a/src/web/start.rs b/src/web/start.rs index 1ea55b8..44db089 100644 --- a/src/web/start.rs +++ b/src/web/start.rs @@ -53,7 +53,7 @@ pub fn run() { let mut lb = pingora_proxy::http_proxy_service(&server.configuration, ll); lb.add_tcp("0.0.0.0:6193"); - // server.add_service(lb); + server.add_service(lb); // server.add_service(background.task()); server.run_forever();