mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
initial commit
This commit is contained in:
@@ -7,6 +7,7 @@ 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>(_: T) -> &'static str {
|
||||
@@ -23,13 +24,14 @@ pub struct LB {
|
||||
// pub upstreams_map: Arc<Mutex<HashMap<String, Vec<(String, u16)>>>>,
|
||||
// upstreams: DashMap<String, (Vec<(&'static str, u16)>, AtomicUsize)>,
|
||||
// pub upstreams_map: DashMap<String, Vec<(String, u16)>>,
|
||||
pub upstreams_map: DashMap<String, (Vec<(String, u16)>, AtomicUsize)>,
|
||||
// pub upstreams_maps: DashMap<String, Arc<LoadBalancer<RoundRobin>>>,
|
||||
pub upstreams_map: DashMap<String, (Vec<(String, u16)>, AtomicUsize)>,
|
||||
}
|
||||
|
||||
pub trait GetHost {
|
||||
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);
|
||||
}
|
||||
|
||||
impl GetHost for LB {
|
||||
@@ -83,6 +85,15 @@ impl GetHost for LB {
|
||||
// let elapsed = now.elapsed();
|
||||
// println!("Elapsed: {:.2?}", elapsed);
|
||||
}
|
||||
|
||||
fn discover_hosts(&mut self) {
|
||||
self.set_host("myip.netangels.net", "192.168.1.1", 8000);
|
||||
self.set_host("myip.netangels.net", "127.0.0.1", 8000);
|
||||
self.set_host("myip.netangels.net", "127.0.0.2", 8000);
|
||||
self.set_host("polo.netangels.net", "192.168.1.1", 8000);
|
||||
self.set_host("polo.netangels.net", "192.168.1.10", 8000);
|
||||
self.set_host("glop.netangels.net", "192.168.1.20", 8000);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -173,56 +184,3 @@ impl ProxyHttp for LB {
|
||||
// info!("{}, response code: {response_code}", self.request_summary(session, ctx));
|
||||
// }
|
||||
}
|
||||
|
||||
/*
|
||||
pub struct SD;
|
||||
|
||||
#[async_trait]
|
||||
impl ServiceDiscovery for SD {
|
||||
async fn discover(&self) -> Result<(BTreeSet<Backend>, HashMap<u64, bool>)> {
|
||||
let addrs = read_upstreams_from_file()?;
|
||||
|
||||
/*
|
||||
let addr2 = read_upstreams_from_file_d()?;
|
||||
let mut hmn = HashMap::new();
|
||||
for (k, v) in addr2 {
|
||||
let vl = v
|
||||
.into_iter()
|
||||
.map(|addr| Backend {
|
||||
addr,
|
||||
weight: 1,
|
||||
ext: Default::default(),
|
||||
})
|
||||
.collect::<BTreeSet<_>>();
|
||||
hmn.insert(k, vl);
|
||||
}
|
||||
println!("{:?}", hmn);
|
||||
println!("================== {:?}", addrs);
|
||||
*/
|
||||
|
||||
let backends = addrs
|
||||
.into_iter()
|
||||
.map(|addr| Backend {
|
||||
addr,
|
||||
weight: 1,
|
||||
ext: Default::default(),
|
||||
})
|
||||
.collect::<BTreeSet<_>>();
|
||||
Ok((backends, HashMap::new()))
|
||||
}
|
||||
}
|
||||
|
||||
fn read_upstreams_from_file() -> Result<Vec<SocketAddr>> {
|
||||
let contents = std::fs::read_to_string("upstreams.txt").map_err(|e| Error::because(ErrorType::InternalError, "reading upstreams file", e))?;
|
||||
let addrs = contents
|
||||
.lines()
|
||||
.filter(|line| !line.trim().is_empty())
|
||||
.map(|line| {
|
||||
line.trim()
|
||||
.parse::<SocketAddr>()
|
||||
.map_err(|e| Error::because(ErrorType::InternalError, "parsing upstream address", e))
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(addrs)
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::web::proxyhttp::{GetHost, LB};
|
||||
use dashmap::DashMap;
|
||||
use pingora_core::prelude::background_service;
|
||||
use pingora_core::server::Server;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
/*
|
||||
@@ -45,22 +46,15 @@ pub fn run() {
|
||||
upstreams_map,
|
||||
// upstreams_maps: DashMap::new(),
|
||||
};
|
||||
add_hosts(&mut ll);
|
||||
|
||||
let background = background_service("load balancer", ll.discover_hosts());
|
||||
background.task();
|
||||
|
||||
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(background);
|
||||
// server.add_service(lb);
|
||||
// server.add_service(background.task());
|
||||
|
||||
server.run_forever();
|
||||
}
|
||||
|
||||
fn add_hosts(lb: &mut LB) {
|
||||
lb.set_host("myip.netangels.net", "192.168.1.1", 8000);
|
||||
lb.set_host("myip.netangels.net", "127.0.0.1", 8000);
|
||||
lb.set_host("myip.netangels.net", "127.0.0.2", 8000);
|
||||
lb.set_host("polo.netangels.net", "192.168.1.1", 8000);
|
||||
lb.set_host("polo.netangels.net", "192.168.1.10", 8000);
|
||||
lb.set_host("glop.netangels.net", "192.168.1.20", 8000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user