From 8dc4a0aade8905abdf61fcf2463cb0be38e6fde5 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 10 Jul 2023 21:54:28 +0200 Subject: [PATCH] user controlled timeout for net monitor --- python/libpt/networking/monitoring/uptime.pyi | 6 ++++-- src/bin/main/args.rs | 6 +++++- src/bin/main/mod.rs | 7 ++++--- src/networking/mod.rs | 2 +- src/networking/monitoring/uptime.rs | 18 +++++++++++------- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/python/libpt/networking/monitoring/uptime.pyi b/python/libpt/networking/monitoring/uptime.pyi index bedece7..f6dc4f8 100644 --- a/python/libpt/networking/monitoring/uptime.pyi +++ b/python/libpt/networking/monitoring/uptime.pyi @@ -21,10 +21,12 @@ class UptimeStatus: success_ratio_target: int """ the number of reachable [`urls`](UptimeStatus::urls) """ reachable: int + """ set a timeout (in ms) """ + timeout: int """URL list cant be ported to python, use UptimeStatus.urls()""" __urls: ... - def __init__(self, success_ratio_target: int, url_strs: list[str]) -> None: + def __init__(self, success_ratio_target: int, url_strs: list[str], timeout: int) -> None: """ create a new UptimeStatus and check it @@ -71,7 +73,7 @@ class UptimeStatus: """ ... -def continuous_uptime_monitor(success_ratio_target: int, urls: list[str], interval: int) -> None: +def continuous_uptime_monitor(success_ratio_target: int, urls: list[str], interval: int, timeout: int) -> None: """ Uptime monitor diff --git a/src/bin/main/args.rs b/src/bin/main/args.rs index b8f6526..d06192f 100644 --- a/src/bin/main/args.rs +++ b/src/bin/main/args.rs @@ -99,7 +99,11 @@ pub enum NetCommands { /// Don't check for default URLs #[clap(short, long)] - no_default: bool + no_default: bool, + + /// set a timeout (in ms) + #[clap(short, long, default_value_t = 100)] + timeout: u64 }, /// discover hosts in your network diff --git a/src/bin/main/mod.rs b/src/bin/main/mod.rs index 5a68971..3141a6b 100644 --- a/src/bin/main/mod.rs +++ b/src/bin/main/mod.rs @@ -75,7 +75,8 @@ pub fn net(cli: &Cli, command: NetCommands) { repeat, success_ratio, extra_urls, - no_default + no_default, + timeout } => { let urls: Vec; if no_default { @@ -91,9 +92,9 @@ pub fn net(cli: &Cli, command: NetCommands) { } let _verbose = cli.verbose.log_level().is_some(); if repeat > 0 { - uptime::continuous_uptime_monitor(success_ratio, urls, repeat * 1000); + uptime::continuous_uptime_monitor(success_ratio, urls, repeat * 1000, timeout); } else { - let status = uptime::UptimeStatus::new(success_ratio, &urls); + let status = uptime::UptimeStatus::new(success_ratio, &urls, timeout); println!("{}", status); } } diff --git a/src/networking/mod.rs b/src/networking/mod.rs index cae88ee..a000ce7 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -22,7 +22,7 @@ pub mod monitoring; //// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// /// how long to wait for a remove server to respond in ms -pub const REQUEST_TIMEOUT: u64 = 2000; +pub const DEFAULT_REQUEST_TIMEOUT: u64 = 2000; //// STATICS /////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/networking/monitoring/uptime.rs b/src/networking/monitoring/uptime.rs index 8639d7d..7474fb3 100644 --- a/src/networking/monitoring/uptime.rs +++ b/src/networking/monitoring/uptime.rs @@ -62,13 +62,15 @@ pub struct UptimeStatus { pub reachable: usize, /// which urls to check in [`check()`](UptimeStatus::check) pub urls: Vec, + /// timeout length for requests (in ms) + pub timeout: u64 } //// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// /// Main implementation impl UptimeStatus { /// ## create a new `UptimeStatus` and perform it's check - pub fn new(success_ratio_target: u8, url_strs: &Vec) -> Self { + pub fn new(success_ratio_target: u8, url_strs: &Vec, timeout: u64) -> Self { assert!(success_ratio_target <= 100); let mut status = UptimeStatus { success: false, @@ -76,6 +78,7 @@ impl UptimeStatus { success_ratio_target, reachable: 0, urls: Vec::new(), + timeout }; for s in url_strs { let url = reqwest::Url::from_str(&s); @@ -102,7 +105,7 @@ impl UptimeStatus { self.reachable = 0; self.urls.iter().for_each(|url| { let client = reqwest::blocking::Client::builder() - .timeout(Duration::from_millis(crate::networking::REQUEST_TIMEOUT)) + .timeout(Duration::from_millis(self.timeout)) .build() .expect("could not build a client for https requests"); let response = client.get(url.clone()).send(); @@ -149,8 +152,8 @@ impl UptimeStatus { impl UptimeStatus { /// calls [`new()`](UptimeStatus::new) with python compatible arguments #[new] - pub fn py_new(success_ratio_target: u8, url_strs: Vec) -> Self { - Self::new(success_ratio_target, &url_strs) + pub fn py_new(success_ratio_target: u8, url_strs: Vec, timeout: u64) -> Self { + Self::new(success_ratio_target, &url_strs, timeout) } /// Same as [`check()`](UptimeStatus::check) @@ -237,7 +240,7 @@ impl fmt::Display for UptimeStatus { /// On change of status, an update will be logged at [INFO Level](log::Level::Info), containing /// information on your current status, including timestamps of the last up/down time and durations /// since. -pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec, interval: u64) { +pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec, interval: u64, timeout: u64) { if urls.len() == 0 { error!("No URLs provided. There is nothing to monitor."); return; @@ -246,7 +249,7 @@ pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec, in let interval = std::time::Duration::from_millis(interval); let mut last_downtime: Option = None; let mut last_uptime: Option = None; - let mut status = UptimeStatus::new(success_ratio_target, &urls); + let mut status = UptimeStatus::new(success_ratio_target, &urls, timeout); let mut last_was_up: bool = false; let mut last_ratio: u8 = status.success_ratio; loop { @@ -292,10 +295,11 @@ pub fn py_continuous_uptime_monitor( success_ratio_target: u8, urls: Vec, interval: u64, + timeout: u64 ) -> PyResult<()>{ // execute the function in a different thread let _th = std::thread::spawn(move || { - continuous_uptime_monitor(success_ratio_target, urls, interval); + continuous_uptime_monitor(success_ratio_target, urls, interval, timeout); }); loop { Python::check_signals(py)?;