initial commit

This commit is contained in:
Ara Sadoyan
2025-02-08 15:05:33 +01:00
parent 2cb37d720e
commit 5da70255a5
3 changed files with 8 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
mod web; mod web;
fn main() { fn main() {
web::start::run() web::start::run();
} }

View File

@@ -7,13 +7,11 @@ use pingora_http::{RequestHeader, ResponseHeader};
use pingora_proxy::{ProxyHttp, Session}; use pingora_proxy::{ProxyHttp, Session};
use std::any::type_name; use std::any::type_name;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[allow(dead_code)] #[allow(dead_code)]
pub fn typeoff<T>(_: T) -> &'static str { pub fn typeoff<T>(_: T) {
let to = type_name::<T>(); let to = type_name::<T>();
println!("{:?}", to); println!("{:?}", to);
to
} }
// pub struct LB(pub Arc<LoadBalancer<RoundRobin>>); // pub struct LB(pub Arc<LoadBalancer<RoundRobin>>);
@@ -28,14 +26,15 @@ pub struct LB {
pub upstreams_map: DashMap<String, (Vec<(String, u16)>, AtomicUsize)>, pub upstreams_map: DashMap<String, (Vec<(String, u16)>, AtomicUsize)>,
} }
#[async_trait]
pub trait GetHost { 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 set_host(&mut self, peer: &str, host: &str, port: u16);
fn discover_hosts(&mut self); fn discover_hosts(&mut self);
} }
#[async_trait]
impl GetHost for LB { 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); // println!("{:?}", self.upstreams_map);
// let entry = self.upstreams_map.get(peer)?; // let entry = self.upstreams_map.get(peer)?;
// let first = entry.value().first()?; // 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<Box<HttpPeer>> { async fn upstream_peer(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<Box<HttpPeer>> {
let host_name = session.req_header().headers.get("host"); let host_name = session.req_header().headers.get("host");
let ddr = self.get_host(host_name.unwrap().to_str().unwrap()); let ddr = self.get_host(host_name.unwrap().to_str().unwrap());
match ddr { match ddr.await {
Some((host, port)) => { Some((host, port)) => {
let peer = Box::new(HttpPeer::new((host, port), false, "".to_string())); let peer = Box::new(HttpPeer::new((host, port), false, "".to_string()));
Ok(peer) Ok(peer)

View File

@@ -53,7 +53,7 @@ pub fn run() {
let mut lb = pingora_proxy::http_proxy_service(&server.configuration, ll); let mut lb = pingora_proxy::http_proxy_service(&server.configuration, ll);
lb.add_tcp("0.0.0.0:6193"); lb.add_tcp("0.0.0.0:6193");
// server.add_service(lb); server.add_service(lb);
// server.add_service(background.task()); // server.add_service(background.task());
server.run_forever(); server.run_forever();