mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 06:48:37 +08:00
Intermediate v2
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
use dashmap::DashMap;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use crate::utils::tools::*;
|
||||
use tokio::sync::RwLockReadGuard;
|
||||
|
||||
/*
|
||||
#[allow(dead_code)]
|
||||
pub fn dashmaps(map1: &RwLockWriteGuard<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>, map2: &DashMap<String, (Vec<(String, u16)>, AtomicUsize)>) -> bool {
|
||||
pub fn dashmaps(map1: &RwLockWriteGuard<UpstreamMap>, map2: &UpstreamMap) -> bool {
|
||||
if map1.len() != map2.len() {
|
||||
return false;
|
||||
}
|
||||
@@ -26,7 +25,7 @@ pub fn dashmaps(map1: &RwLockWriteGuard<DashMap<String, (Vec<(String, u16)>, Ato
|
||||
*/
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn dm(map1: &RwLockReadGuard<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>, map2: &DashMap<String, (Vec<(String, u16)>, AtomicUsize)>) -> bool {
|
||||
pub fn dm(map1: &RwLockReadGuard<UpstreamMap>, map2: &UpstreamMap) -> bool {
|
||||
if map1.len() != map2.len() {
|
||||
return false; // Different number of keys
|
||||
}
|
||||
|
||||
@@ -20,23 +20,23 @@ pub struct APIUpstreamProvider;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Discovery {
|
||||
async fn run(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>);
|
||||
async fn run(&self, tx: Sender<UpstreamMap>);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Discovery for APIUpstreamProvider {
|
||||
async fn run(&self, toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
async fn run(&self, toreturn: Sender<UpstreamMap>) {
|
||||
webserver::run_server(toreturn).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Discovery for FromFileProvider {
|
||||
async fn run(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
async fn run(&self, tx: Sender<UpstreamMap>) {
|
||||
tokio::spawn(watch_file(self.path.clone(), tx.clone()));
|
||||
}
|
||||
}
|
||||
pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
pub async fn watch_file(fp: String, mut toreturn: Sender<UpstreamMap>) {
|
||||
let file_path = fp.as_str();
|
||||
let parent_dir = Path::new(file_path).parent().unwrap(); // Watch directory, not file
|
||||
let (local_tx, mut local_rx) = tokio::sync::mpsc::channel::<notify::Result<Event>>(1);
|
||||
@@ -77,15 +77,16 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
|
||||
start = Instant::now();
|
||||
println!("Config File changed :=> {:?}", e);
|
||||
|
||||
let _sd = build_upstreams2("etc/upstreams-long.conf", "filepath");
|
||||
let upstreams = build_upstreams2("etc/upstreams-long.conf", "filepath");
|
||||
|
||||
println!("\n\n");
|
||||
for t in _sd.iter() {
|
||||
println!("{} ==>", t.key());
|
||||
for v in t.value().iter() {
|
||||
println!(" {:?}", v)
|
||||
}
|
||||
print_upstreams(&upstreams);
|
||||
|
||||
let host_entry = upstreams.get("myip.netangels.net").unwrap();
|
||||
let path_entry = host_entry.get("/").unwrap();
|
||||
for p in path_entry.value().0.clone() {
|
||||
println!(" {:?}", p);
|
||||
}
|
||||
|
||||
println!("\n\n");
|
||||
|
||||
let snd = build_upstreams(file_path, "filepath");
|
||||
@@ -99,7 +100,7 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn build_upstreams(d: &str, kind: &str) -> DashMap<String, (Vec<(String, u16)>, AtomicUsize)> {
|
||||
pub fn build_upstreams(d: &str, kind: &str) -> UpstreamMap {
|
||||
let upstreams = DashMap::new();
|
||||
let mut contents = d.to_string();
|
||||
match kind {
|
||||
@@ -150,8 +151,8 @@ pub fn build_upstreams(d: &str, kind: &str) -> DashMap<String, (Vec<(String, u16
|
||||
upstreams
|
||||
}
|
||||
|
||||
pub fn build_upstreams2(d: &str, kind: &str) -> DashMap<String, Vec<UpstreamsStruct>> {
|
||||
let upstreams: DashMap<String, Vec<UpstreamsStruct>> = DashMap::new();
|
||||
pub fn build_upstreams2(d: &str, kind: &str) -> UpstresmDashMap {
|
||||
let upstreams: UpstresmDashMap = DashMap::new();
|
||||
let mut contents = d.to_string();
|
||||
match kind {
|
||||
"filepath" => {
|
||||
@@ -176,7 +177,7 @@ pub fn build_upstreams2(d: &str, kind: &str) -> DashMap<String, Vec<UpstreamsStr
|
||||
continue;
|
||||
};
|
||||
|
||||
let Some(ssl) = crate::utils::tools::string_to_bool(parts.next()) else {
|
||||
let Some(ssl) = string_to_bool(parts.next()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
@@ -201,13 +202,13 @@ pub fn build_upstreams2(d: &str, kind: &str) -> DashMap<String, Vec<UpstreamsStr
|
||||
let Ok(port) = port_str.parse::<u16>() else {
|
||||
continue;
|
||||
};
|
||||
let d = UpstreamsStruct {
|
||||
proto: proto.to_string(),
|
||||
path: path.to_string(),
|
||||
address: (ip.to_string(), port, ssl),
|
||||
atom: AtomicUsize::new(0),
|
||||
};
|
||||
upstreams.entry(hostname.to_string()).or_insert_with(|| Vec::new()).push(d);
|
||||
|
||||
let entry = upstreams.entry(hostname.to_string()).or_insert_with(DashMap::new);
|
||||
entry
|
||||
.entry(path.to_string())
|
||||
.or_insert_with(|| (Vec::new(), AtomicUsize::new(0)))
|
||||
.0
|
||||
.push((ip.to_string(), port, ssl, proto.to_string()));
|
||||
}
|
||||
|
||||
upstreams
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::utils::tools::*;
|
||||
use dashmap::DashMap;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::Arc;
|
||||
@@ -5,15 +6,15 @@ use std::time::Duration;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio::time::interval;
|
||||
|
||||
pub async fn hc(upslist: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>, fullist: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>) {
|
||||
pub async fn hc(upslist: Arc<RwLock<UpstreamMap>>, fullist: Arc<RwLock<UpstreamMap>>) {
|
||||
let mut period = interval(Duration::from_secs(2));
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = period.tick() => {
|
||||
// let before = Instant::now();
|
||||
let totest: DashMap<String, (Vec<(String, u16)>, AtomicUsize)> = DashMap::new();
|
||||
let fclone: DashMap<String, (Vec<(String, u16)>, AtomicUsize)> = DashMap::new();
|
||||
let totest: UpstreamMap = DashMap::new();
|
||||
let fclone: UpstreamMap = DashMap::new();
|
||||
// println!("\nElapsed dash: {:.2?}", before.elapsed());
|
||||
// let before = Instant::now();
|
||||
{
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
use dashmap::DashMap;
|
||||
use std::any::type_name;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct UpstreamsStruct {
|
||||
pub proto: String,
|
||||
pub path: String,
|
||||
pub address: (String, u16, bool),
|
||||
pub atom: AtomicUsize,
|
||||
pub fn print_upstreams(upstreams: &UpstresmDashMap) {
|
||||
for host_entry in upstreams.iter() {
|
||||
let hostname = host_entry.key();
|
||||
println!("Hostname: {}", hostname);
|
||||
|
||||
for path_entry in host_entry.value().iter() {
|
||||
let path = path_entry.key();
|
||||
println!(" Path: {}", path);
|
||||
|
||||
for (ip, port, ssl, proto) in path_entry.value().0.clone() {
|
||||
println!(" ===> IP: {}, Port: {}, SSL: {}, Proto: {}", ip, port, ssl, proto);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type UpstresmDashMap = DashMap<String, DashMap<String, (Vec<(String, u16, bool, String)>, AtomicUsize)>>;
|
||||
pub type UpstreamMap = DashMap<String, (Vec<(String, u16)>, AtomicUsize)>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn typeoff<T>(_: T) {
|
||||
let to = type_name::<T>();
|
||||
|
||||
Reference in New Issue
Block a user