mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
Structure, dummy API config
This commit is contained in:
@@ -9,31 +9,46 @@ use async_trait::async_trait;
|
|||||||
use notify::event::ModifyKind;
|
use notify::event::ModifyKind;
|
||||||
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
pub struct DSC;
|
pub struct FromFileProvider {
|
||||||
|
pub path: String,
|
||||||
|
}
|
||||||
|
pub struct APIUpstreamProvider {
|
||||||
|
pub api_url: String,
|
||||||
|
}
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Discovery {
|
pub trait Discovery {
|
||||||
async fn discover(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>);
|
async fn run(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Discovery for DSC {
|
impl Discovery for APIUpstreamProvider {
|
||||||
async fn discover(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
async fn run(&self, mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||||
let file_path = "etc/upstreams.conf";
|
loop {
|
||||||
tokio::spawn(watch_file(file_path, tx));
|
let dm: DashMap<String, (Vec<(String, u16)>, AtomicUsize)> = DashMap::new();
|
||||||
|
dm.insert(
|
||||||
|
self.api_url.to_string(),
|
||||||
|
(vec![("192.168.1.1".parse().unwrap(), 8000), ("192.168.1.10".parse().unwrap(), 8000)], AtomicUsize::new(0)),
|
||||||
|
);
|
||||||
|
println!("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
|
||||||
|
let _ = toreturn.send(dm).await.unwrap();
|
||||||
|
println!("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
|
||||||
|
tokio::time::sleep(Duration::from_secs(20)).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub async fn dsc(tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
#[async_trait]
|
||||||
// let file_path = "etc/upstreams.conf";
|
impl Discovery for FromFileProvider {
|
||||||
// tokio::spawn(watch_file(file_path, tx));
|
async fn run(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||||
// }
|
tokio::spawn(watch_file(self.path.clone(), tx.clone()));
|
||||||
|
}
|
||||||
pub async fn watch_file(file_path: &str, mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
}
|
||||||
|
pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||||
|
let file_path = fp.as_str();
|
||||||
let parent_dir = Path::new(file_path).parent().unwrap(); // Watch directory, not file
|
let parent_dir = Path::new(file_path).parent().unwrap(); // Watch directory, not file
|
||||||
let (tx, mut rx) = mpsc::channel::<notify::Result<Event>>(10);
|
let (local_tx, mut local_rx) = tokio::sync::mpsc::channel::<notify::Result<Event>>(1);
|
||||||
|
|
||||||
println!("Watching for changes in {:?}", parent_dir);
|
println!("Watching for changes in {:?}", parent_dir);
|
||||||
let paths = fs::read_dir(parent_dir).unwrap();
|
let paths = fs::read_dir(parent_dir).unwrap();
|
||||||
@@ -49,7 +64,7 @@ pub async fn watch_file(file_path: &str, mut toreturn: Sender<DashMap<String, (V
|
|||||||
move || {
|
move || {
|
||||||
let mut watcher = RecommendedWatcher::new(
|
let mut watcher = RecommendedWatcher::new(
|
||||||
move |res| {
|
move |res| {
|
||||||
let _ = tx.blocking_send(res);
|
let _ = local_tx.blocking_send(res);
|
||||||
},
|
},
|
||||||
Config::default(),
|
Config::default(),
|
||||||
)
|
)
|
||||||
@@ -61,21 +76,28 @@ pub async fn watch_file(file_path: &str, mut toreturn: Sender<DashMap<String, (V
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// loop {
|
||||||
|
// println!(" ---------------------------------------------------------------- ");
|
||||||
|
// thread::sleep(Duration::from_secs(1));
|
||||||
|
// }
|
||||||
let mut start = Instant::now();
|
let mut start = Instant::now();
|
||||||
while let Some(event) = rx.recv().await {
|
|
||||||
|
while let Some(event) = local_rx.recv().await {
|
||||||
match event {
|
match event {
|
||||||
Ok(e) => match e.kind {
|
Ok(e) => match e.kind {
|
||||||
EventKind::Modify(ModifyKind::Data(_)) | EventKind::Create(..) | EventKind::Remove(..) => {
|
EventKind::Modify(ModifyKind::Data(_)) | EventKind::Create(..) | EventKind::Remove(..) => {
|
||||||
if e.paths[0].to_str().unwrap().ends_with("conf") {
|
if e.paths[0].to_str().unwrap().ends_with("conf") {
|
||||||
if start.elapsed() > Duration::from_secs(10) {
|
// if start.elapsed() > Duration::from_secs(10) {
|
||||||
|
if start.elapsed() > Duration::from_secs(2) {
|
||||||
start = Instant::now();
|
start = Instant::now();
|
||||||
println!("Config File changed :=> {:?}", e);
|
println!("Config File changed :=> {:?}", e);
|
||||||
|
|
||||||
let snd = read_upstreams_from_file(file_path);
|
let snd = read_upstreams_from_file(file_path);
|
||||||
let _ = toreturn.send(snd).await.unwrap();
|
let _ = toreturn.send(snd).await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (), //println!("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"),
|
||||||
},
|
},
|
||||||
Err(e) => println!("Watch error: {:?}", e),
|
Err(e) => println!("Watch error: {:?}", e),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// use crate::utils::compare;
|
// use crate::utils::compare;
|
||||||
// use crate::utils::discovery;
|
// use crate::utils::discovery;
|
||||||
use crate::utils::discovery::Discovery;
|
use crate::utils::discovery::{APIUpstreamProvider, Discovery, FromFileProvider};
|
||||||
use crate::utils::*;
|
use crate::utils::*;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
@@ -31,9 +31,21 @@ impl BackgroundService for LB {
|
|||||||
async fn start(&self, mut shutdown: ShutdownWatch) {
|
async fn start(&self, mut shutdown: ShutdownWatch) {
|
||||||
tokio::spawn(healthcheck::hc(self.upstreams.clone(), self.umap_full.clone()));
|
tokio::spawn(healthcheck::hc(self.upstreams.clone(), self.umap_full.clone()));
|
||||||
println!("Starting example background service");
|
println!("Starting example background service");
|
||||||
|
// let (tra, mut rec) = broadcast::channel::<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>(16);
|
||||||
|
|
||||||
let (tx, mut rx) = mpsc::channel::<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>(0);
|
let (tx, mut rx) = mpsc::channel::<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>(0);
|
||||||
// let _ = tokio::spawn(async move { discovery::dsc(tx.clone()).await });
|
let file_load = FromFileProvider {
|
||||||
let _ = tokio::spawn(async move { discovery::DSC.discover(tx.clone()).await });
|
path: "etc/upstreams.conf".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let api_load = APIUpstreamProvider {
|
||||||
|
api_url: "myip.netangels.net".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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 });
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
@@ -44,6 +56,7 @@ impl BackgroundService for LB {
|
|||||||
val = rx.next() => {
|
val = rx.next() => {
|
||||||
match val {
|
match val {
|
||||||
Some(newmap) => {
|
Some(newmap) => {
|
||||||
|
println!("{:?}", newmap);
|
||||||
let umap_work = self.upstreams.write().await;
|
let umap_work = self.upstreams.write().await;
|
||||||
let umap_full = self.umap_full.write().await;
|
let umap_full = self.umap_full.write().await;
|
||||||
if !compare::dashmaps(&umap_full, &newmap) {
|
if !compare::dashmaps(&umap_full, &newmap) {
|
||||||
|
|||||||
Reference in New Issue
Block a user