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:
Ara Sadoyan
2025-03-16 14:06:29 +01:00
parent 3901b246b3
commit 6cc72c8b48
13 changed files with 405 additions and 288 deletions

View File

@@ -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

View File

@@ -1,37 +1,51 @@
use crate::utils::tools::*;
use crate::web::proxyhttp::LB;
use clap::{arg, Parser};
use dashmap::DashMap;
use log::info;
use pingora_core::prelude::background_service;
use pingora_core::server::Server;
use std::sync::Arc;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
address: String,
#[arg(short, long)]
port: String,
}
pub fn run() {
env_logger::init();
let mut server = Server::new(None).unwrap();
server.bootstrap();
let upstreams_map: UpstreamMap = DashMap::new();
let config = Arc::new(upstreams_map);
let umap_full: UpstreamMap = DashMap::new();
let fconfig = Arc::new(umap_full);
let uf: UpstreamsDashMap = DashMap::new();
let ff: UpstreamsDashMap = DashMap::new();
let uf_config = Arc::new(uf);
let ff_config = Arc::new(ff);
let lb = LB {
upstreams: config.clone(),
umap_full: fconfig.clone(),
ump_upst: uf_config.clone(),
ump_full: ff_config.clone(),
};
let bg = LB {
upstreams: config.clone(),
umap_full: fconfig.clone(),
ump_upst: uf_config.clone(),
ump_full: ff_config.clone(),
};
let bg_srvc = background_service("bgsrvc", bg);
let mut proxy = pingora_proxy::http_proxy_service(&server.configuration, lb);
proxy.add_tcp("0.0.0.0:6193");
let args = Args::parse();
let addr = format!("{}:{}", args.address, args.port);
proxy.add_tcp(&addr);
server.add_service(proxy);
server.add_service(bg_srvc);
info!("Starting Gazan server on {}, port : {} !", args.address, args.port);
server.run_forever();
}

View File

@@ -3,22 +3,17 @@ use axum::body::Body;
use axum::http::{Response, StatusCode};
use axum::response::IntoResponse;
use axum::routing::{delete, get, head, post, put};
use axum::{Json, Router};
use dashmap::DashMap;
use axum::Router;
use futures::channel::mpsc::Sender;
use futures::SinkExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::AtomicUsize;
use tokio::net::TcpListener;
#[derive(Debug, Serialize, Deserialize)]
struct UpstreamData {
servers: Vec<(String, u16)>,
counter: usize,
}
// struct UpstreamData {
// servers: UpstreamsDashMap,
// }
pub async fn run_server(mut toreturn: Sender<UpstreamMap>) {
#[allow(unused_mut)]
pub async fn run_server(mut toreturn: Sender<UpstreamsDashMap>) {
let mut tr = toreturn.clone();
let app = Router::new()
.route("/{*wildcard}", get(getconfig))
@@ -29,28 +24,18 @@ pub async fn run_server(mut toreturn: Sender<UpstreamMap>) {
.route(
"/conf",
post(|up: String| async move {
let serverlist = crate::utils::discovery::build_upstreams(up.as_str(), "content");
let serverlist = crate::utils::parceyaml::load_yaml_to_dashmap(up.as_str(), "content");
let _ = tr.send(serverlist).await.unwrap();
Response::builder().status(StatusCode::CREATED).body(Body::from("Config, conf file, updated!\n")).unwrap()
})
.with_state("state"),
)
.route(
"/json",
post(|Json(payload): Json<HashMap<String, UpstreamData>>| async move {
let upstreams = DashMap::new();
for (key, value) in payload {
upstreams.insert(key, (value.servers, AtomicUsize::new(value.counter)));
}
let _ = toreturn.send(upstreams).await.unwrap();
Response::builder().status(StatusCode::CREATED).body(Body::from("Config, json, updated!\n")).unwrap()
}),
);
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("Axum API server running on port 3000");
axum::serve(listener, app).await.unwrap();
}
#[allow(dead_code)]
async fn getconfig() -> impl IntoResponse {
"Hello from Axum API inside Pingora!\n".to_string();
Response::builder().status(StatusCode::BAD_GATEWAY).body(Body::from("No live upstream found!\n")).unwrap()