mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 23:08:40 +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:
@@ -5,60 +5,52 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::time::interval;
|
||||
|
||||
pub async fn hc(upslist: Arc<UpstreamMap>, fullist: Arc<UpstreamMap>) {
|
||||
pub async fn hc2(upslist: Arc<UpstreamsDashMap>, fullist: Arc<UpstreamsDashMap>) {
|
||||
let mut period = interval(Duration::from_secs(2));
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = period.tick() => {
|
||||
// let before = Instant::now();
|
||||
let totest: UpstreamMap = DashMap::new();
|
||||
let fclone: UpstreamMap = DashMap::new();
|
||||
// println!("\nElapsed dash: {:.2?}", before.elapsed());
|
||||
// let before = Instant::now();
|
||||
{
|
||||
for v in fullist.iter() {
|
||||
fclone.insert(v.key().clone(), (v.value().0.clone(), AtomicUsize::new(0)));
|
||||
}
|
||||
} // lock releases when scope ends
|
||||
// println!("Elapsed full: {:.2?}", before.elapsed());
|
||||
let totest : UpstreamsDashMap = DashMap::new();
|
||||
let fclone : UpstreamsDashMap = clone_dashmap(&fullist);
|
||||
for val in fclone.iter() {
|
||||
let mut newvec = vec![];
|
||||
for hostport in val.value().0.clone(){
|
||||
let hostpart = hostport.0.split('/').last().unwrap(); // For later use
|
||||
let url = format!("http://{}:{}", hostpart, hostport.1);
|
||||
let resp = http_request(url.as_str(), "GET", "").await;
|
||||
match resp{
|
||||
true => {
|
||||
newvec.push((hostpart.to_string(), hostport.1));
|
||||
},
|
||||
false => {
|
||||
println!("Dead upstream. Host: {}, Upstream: {}:{} ",val.key(), hostpart.to_string(), hostport.1 );
|
||||
let host = val.key();
|
||||
let inner = DashMap::new();
|
||||
for path_entry in val.value().iter() {
|
||||
// let inner = DashMap::new();
|
||||
let path = path_entry.key();
|
||||
let mut innervec= Vec::new();
|
||||
for k in path_entry.value().0.iter().enumerate() {
|
||||
let (ip, port, ssl, _proto) = k.1;
|
||||
let mut _pref = "";
|
||||
match ssl {
|
||||
true => _pref = "https://",
|
||||
false => _pref = "http://",
|
||||
}
|
||||
let link = format!("{}{}:{}{}", _pref, ip, port, path);
|
||||
let resp = http_request(link.as_str(), "HEAD", "").await;
|
||||
match resp {
|
||||
true => {
|
||||
innervec.push(k.1.clone());
|
||||
}
|
||||
false => {
|
||||
println!("Dead Upstream {}, Link: {}",k.0, link);
|
||||
}
|
||||
}
|
||||
}
|
||||
inner.insert(path.clone().to_owned(), (innervec, AtomicUsize::new(0)));
|
||||
}
|
||||
totest.insert(val.key().clone(), (newvec, AtomicUsize::new(0)));
|
||||
totest.insert(host.clone(), inner);
|
||||
}
|
||||
// let before = Instant::now();
|
||||
{
|
||||
if !crate::utils::compare::dm(&upslist, &totest) {
|
||||
println!("Dashmaps not matched, synchronizing");
|
||||
upslist.clear();
|
||||
for (k, v) in totest { // loop takes the ownership
|
||||
println!("Host: {}", k);
|
||||
for vv in &v.0 {
|
||||
println!(" :===> {:?}", vv);
|
||||
}
|
||||
upslist.insert(k, v);
|
||||
}
|
||||
}
|
||||
if ! compare_dashmaps(&totest, &upslist){
|
||||
print_upstreams(&totest);
|
||||
clone_dashmap_into(&totest, &upslist);
|
||||
}
|
||||
// println!("Elapsed upsl: {:.2?}", before.elapsed());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn http_request(url: &str, method: &str, payload: &str) -> bool {
|
||||
let client = reqwest::Client::new();
|
||||
let to = Duration::from_secs(1);
|
||||
@@ -83,6 +75,13 @@ async fn http_request(url: &str, method: &str, payload: &str) -> bool {
|
||||
}
|
||||
}
|
||||
}
|
||||
"HEAD" => {
|
||||
let response = client.head(url).timeout(to).send().await;
|
||||
match response {
|
||||
Ok(r) => 100 <= r.status().as_u16() && r.status().as_u16() < 500,
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user