mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
New inmplementations, big commit :
1. Nested upstreams with params 2. SSL upstream support 3. Upstreams move to yaml format 4. Command line start arguments
This commit is contained in:
@@ -11,59 +11,46 @@ use pingora_core::server::ShutdownWatch;
|
||||
use pingora_core::services::background::BackgroundService;
|
||||
use pingora_http::{RequestHeader, ResponseHeader};
|
||||
use pingora_proxy::{ProxyHttp, Session};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
// use tokio::time::Instant;
|
||||
|
||||
pub struct LB {
|
||||
pub upstreams: Arc<UpstreamMap>,
|
||||
pub umap_full: Arc<UpstreamMap>,
|
||||
pub ump_upst: Arc<UpstreamsDashMap>,
|
||||
pub ump_full: Arc<UpstreamsDashMap>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl BackgroundService for LB {
|
||||
async fn start(&self, mut shutdown: ShutdownWatch) {
|
||||
println!("Starting example background service");
|
||||
|
||||
let (tx, mut rx) = mpsc::channel::<UpstreamMap>(0);
|
||||
let file_load = FromFileProvider {
|
||||
path: "etc/upstreams.conf".to_string(),
|
||||
let (tux, mut rux) = mpsc::channel::<UpstreamsDashMap>(0);
|
||||
let file_load2 = FromFileProvider {
|
||||
path: "etc/upstreams.yaml".to_string(),
|
||||
};
|
||||
|
||||
let api_load = APIUpstreamProvider;
|
||||
|
||||
let tx_file = tx.clone();
|
||||
let tx_api = tx.clone();
|
||||
let _ = tokio::spawn(async move { api_load.run(tx_api).await });
|
||||
let _ = tokio::spawn(async move { file_load.run(tx_file).await });
|
||||
let up = self.upstreams.clone();
|
||||
let fu = self.umap_full.clone();
|
||||
let _ = tokio::spawn(async move { healthcheck::hc(up, fu).await });
|
||||
let tux_file = tux.clone();
|
||||
let tux_api = tux.clone();
|
||||
let _ = tokio::spawn(async move { file_load2.start(tux_file).await });
|
||||
let _ = tokio::spawn(async move { api_load.start(tux_api).await });
|
||||
let uu = self.ump_upst.clone();
|
||||
let ff = self.ump_full.clone();
|
||||
let _ = tokio::spawn(async move { healthcheck::hc2(uu, ff).await });
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = shutdown.changed() => {
|
||||
break;
|
||||
}
|
||||
val = rx.next() => {
|
||||
val = rux.next() => {
|
||||
match val {
|
||||
Some(newmap) => {
|
||||
match compare::dm(&self.umap_full, &newmap) {
|
||||
false => {
|
||||
self.upstreams.clear();
|
||||
self.umap_full.clear();
|
||||
for (k,v) in newmap {
|
||||
println!("Host: {}", k);
|
||||
// <UpstreamMap
|
||||
for vv in v.0.clone() {
|
||||
println!(" ===> {:?}", vv);
|
||||
}
|
||||
self.upstreams.insert(k.clone(), (v.0.clone(), AtomicUsize::new(0))); // No need for extra vec!
|
||||
self.umap_full.insert(k, (v.0, AtomicUsize::new(0))); // Use `value.0` directly
|
||||
}
|
||||
}
|
||||
true => {
|
||||
}
|
||||
Some(ss) => {
|
||||
let foo = compare_dashmaps(&*self.ump_full, &ss);
|
||||
if !foo {
|
||||
clone_dashmap_into(&ss, &self.ump_full);
|
||||
clone_dashmap_into(&ss, &self.ump_upst);
|
||||
print_upstreams(&self.ump_full);
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
@@ -76,12 +63,19 @@ impl BackgroundService for LB {
|
||||
|
||||
#[async_trait]
|
||||
pub trait GetHost {
|
||||
async fn get_host(&self, peer: &str) -> Option<(String, u16)>;
|
||||
async fn get_host(&self, peer: &str, path: &str, upgrade: bool) -> Option<(String, u16, bool, String)>;
|
||||
}
|
||||
#[async_trait]
|
||||
impl GetHost for LB {
|
||||
async fn get_host(&self, peer: &str) -> Option<(String, u16)> {
|
||||
let x = if let Some(entry) = self.upstreams.get(peer) {
|
||||
async fn get_host(&self, peer: &str, path: &str, upgrade: bool) -> Option<(String, u16, bool, String)> {
|
||||
let mut _proto = "";
|
||||
if upgrade {
|
||||
_proto = "wsoc";
|
||||
} else {
|
||||
_proto = "http"
|
||||
}
|
||||
let host_entry = self.ump_upst.get(peer).unwrap();
|
||||
let x = if let Some(entry) = host_entry.get(path) {
|
||||
let (servers, index) = entry.value();
|
||||
if servers.is_empty() {
|
||||
return None;
|
||||
@@ -105,16 +99,17 @@ impl ProxyHttp for LB {
|
||||
let host_name = session.req_header().headers.get("host");
|
||||
match host_name {
|
||||
Some(host) => {
|
||||
let h = host.to_str().unwrap().split(':').collect::<Vec<&str>>();
|
||||
let ddr = self.get_host(h[0]);
|
||||
let header_host = host.to_str().unwrap().split(':').collect::<Vec<&str>>();
|
||||
|
||||
let ddr = self.get_host(header_host[0], session.req_header().uri.path(), session.is_upgrade_req());
|
||||
match ddr.await {
|
||||
Some((host, port)) => {
|
||||
let peer = Box::new(HttpPeer::new((host, port), false, String::new()));
|
||||
Some((host, port, ssl, _proto)) => {
|
||||
let peer = Box::new(HttpPeer::new((host, port), ssl, String::new()));
|
||||
// info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
|
||||
Ok(peer)
|
||||
}
|
||||
None => {
|
||||
warn!("Returning default list => {:?}", ("127.0.0.1", 3000));
|
||||
warn!("Returning default list => {:?}, {:?}", host_name, session.req_header().uri);
|
||||
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
|
||||
// info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
|
||||
Ok(peer)
|
||||
@@ -122,26 +117,12 @@ impl ProxyHttp for LB {
|
||||
}
|
||||
}
|
||||
None => {
|
||||
warn!("Returning default list => {:?}", ("127.0.0.1", 3000));
|
||||
warn!("Returning default list => {:?}, {:?}", host_name, session.req_header().uri);
|
||||
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
|
||||
// info!("{:?}, Time => {:.2?}", session.request_summary(), before.elapsed());
|
||||
Ok(peer)
|
||||
}
|
||||
}
|
||||
/*
|
||||
let ddr = self.get_host(host_name.unwrap().to_str().unwrap());
|
||||
match ddr.await {
|
||||
Some((host, port)) => {
|
||||
let peer = Box::new(HttpPeer::new((host, port), false, String::new()));
|
||||
Ok(peer)
|
||||
}
|
||||
None => {
|
||||
println!("Returning default list => {:?}", ("127.0.0.1", 3000));
|
||||
let peer = Box::new(HttpPeer::new(("127.0.0.1", 3000), false, String::new()));
|
||||
Ok(peer)
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
async fn request_filter(&self, _session: &mut Session, _ctx: &mut Self::CTX) -> pingora_core::Result<bool>
|
||||
where
|
||||
|
||||
Reference in New Issue
Block a user