mirror of
https://github.com/thegeneralist01/p2p-failover
synced 2026-01-09 14:50:29 +01:00
make checking alive nodes multithreaded
This commit is contained in:
parent
5b4bc54c1f
commit
bc39e5caaf
4 changed files with 113 additions and 37 deletions
51
Cargo.lock
generated
51
Cargo.lock
generated
|
|
@ -254,6 +254,21 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.31"
|
||||
|
|
@ -261,6 +276,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -269,6 +285,34 @@ version = "0.3.31"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.31"
|
||||
|
|
@ -287,10 +331,16 @@ version = "0.3.31"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -882,6 +932,7 @@ dependencies = [
|
|||
"anyhow",
|
||||
"chrono",
|
||||
"dateparser",
|
||||
"futures",
|
||||
"notify",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
|
|
|||
|
|
@ -12,3 +12,4 @@ reqwest = "0.12.12"
|
|||
tokio = { version = "=1.40.0", features = ["full"] }
|
||||
notify = "8.0.0"
|
||||
anyhow = "1.0.97"
|
||||
futures = "0.3.31"
|
||||
|
|
|
|||
92
src/node.rs
92
src/node.rs
|
|
@ -1,11 +1,14 @@
|
|||
use crate::{config::Config, log, node_connections::NodeConnections, process::Process};
|
||||
use futures::future::join_all;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::task;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Node {
|
||||
alive: bool,
|
||||
pub config: Arc<Mutex<Config>>,
|
||||
alives: Vec<bool>,
|
||||
process: Option<Process>,
|
||||
process: Option<Arc<Mutex<Process>>>,
|
||||
pub node_connections: NodeConnections,
|
||||
// tick: u8,
|
||||
// tick_dir: u8,
|
||||
|
|
@ -28,47 +31,67 @@ impl Node {
|
|||
|
||||
/// Returns the amount of alive hosts
|
||||
pub async fn check_hosts(&mut self) -> u8 {
|
||||
let mut alives: u8 = 0;
|
||||
let config = self.config.lock().unwrap();
|
||||
for host in config.ddns.iter().enumerate() {
|
||||
if host.1.name == config.config_metadata.name {
|
||||
let config_metadata_name = self.config.lock().unwrap().config_metadata.name.clone();
|
||||
let alives = Arc::new(Mutex::new(0u8));
|
||||
let mut handles = Vec::new();
|
||||
|
||||
for (index, host) in self.config.lock().unwrap().ddns.iter().enumerate() {
|
||||
if host.name == config_metadata_name {
|
||||
continue;
|
||||
}
|
||||
|
||||
self.alives[host.0] = false;
|
||||
self.alives[index] = false;
|
||||
let host_clone = host.clone();
|
||||
let alives_clone = Arc::clone(&alives);
|
||||
let mut node_connections = self.node_connections.clone();
|
||||
|
||||
log!(
|
||||
"Checking: {}:{}",
|
||||
if host.1.preference == 0 {
|
||||
&host.1.ddns
|
||||
let handle = task::spawn(async move {
|
||||
let host_name = host_clone.name.clone();
|
||||
let host_priority = host_clone.priority;
|
||||
|
||||
log!(
|
||||
"Checking: {}:{}",
|
||||
if host_clone.preference == 0 {
|
||||
&host_clone.ddns
|
||||
} else {
|
||||
&host_clone.ip
|
||||
},
|
||||
&host_clone.port
|
||||
);
|
||||
|
||||
let alive = task::spawn_blocking(move || node_connections.ping(&host_clone))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if alive {
|
||||
log!(
|
||||
"-> Alive: host \"{}\" with priority {}",
|
||||
host_name,
|
||||
host_priority,
|
||||
);
|
||||
let mut count = alives_clone.lock().unwrap();
|
||||
*count += 1;
|
||||
} else {
|
||||
&host.1.ip
|
||||
},
|
||||
&host.1.port,
|
||||
);
|
||||
let alive = self.node_connections.ping(host.1);
|
||||
if alive {
|
||||
log!(
|
||||
"-> Alive: host \"{}\" with priority {}",
|
||||
host.1.name,
|
||||
host.1.priority
|
||||
);
|
||||
alives += 1;
|
||||
} else {
|
||||
log!(
|
||||
"-> Host \"{}\" with priority {} is dead",
|
||||
host.1.name,
|
||||
host.1.priority
|
||||
);
|
||||
}
|
||||
self.alives[host.0] = alive;
|
||||
log!(
|
||||
"-> Host \"{}\" with priority {} is dead",
|
||||
host_name,
|
||||
host_priority,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
join_all(handles).await;
|
||||
|
||||
let alives = *alives.lock().unwrap();
|
||||
alives
|
||||
}
|
||||
|
||||
fn spawn(&mut self) {
|
||||
let process = Process::new(&self.config.lock().unwrap());
|
||||
self.process = Some(process);
|
||||
self.process = Some(Arc::new(Mutex::new(process)));
|
||||
}
|
||||
|
||||
/// Check for config updates and update
|
||||
|
|
@ -152,8 +175,11 @@ impl Node {
|
|||
{
|
||||
// Clean up
|
||||
self.alive = false;
|
||||
if let Some(ref mut p) = self.process {
|
||||
p.kill();
|
||||
if let Some(p) = &self.process {
|
||||
{
|
||||
let mut process = p.lock().unwrap();
|
||||
process.kill();
|
||||
}
|
||||
self.process = None;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,10 +46,7 @@ impl NodeInfo {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn update_config(
|
||||
&mut self,
|
||||
config_self_mutex: Arc<Mutex<Config>>,
|
||||
) -> Result<()> {
|
||||
pub fn update_config(&mut self, config_self_mutex: Arc<Mutex<Config>>) -> Result<()> {
|
||||
if let Some(ref mut streamp) = self.streamp {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let read_stream = streamp.try_clone().unwrap();
|
||||
|
|
@ -145,6 +142,7 @@ impl NodeInfo {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Clone)]
|
||||
pub struct NodeConnections {
|
||||
connections: Vec<Arc<Mutex<NodeInfo>>>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue