Update to preserve resource output order & add latency information
This commit is contained in:
parent
590006a91b
commit
ad4f9f99e1
|
@ -99,6 +99,8 @@ has been explored, so if issues are encountered please [let it be known](https:/
|
|||
- Add `kind = "Http"` to any block that previously stared with `[[http]]`
|
||||
- Add `kind = "Tcp"` to any block that previously stared with `[[tcp]]`
|
||||
- Updated dependencies in Cargo.toml & Cargo.lock
|
||||
- Added call latency to success output
|
||||
- Updated logic to maintain resource order when printing results
|
||||
- v0.5.0
|
||||
- Refactored to produce both binary and library crates
|
||||
- Created a common `Resource` struct to map `TcpResource` and `HttpResources` onto for consumption by `par_iter()` by `rayon`
|
||||
|
|
|
@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
} else {
|
||||
let config_path = std::path::PathBuf::from(&args[1]);
|
||||
let config = std::fs::read_to_string(&config_path)?;
|
||||
let resources: NetworkResources = toml::from_str(&config)?;
|
||||
let mut resources: NetworkResources = toml::from_str(&config)?;
|
||||
resources.check_resources();
|
||||
}
|
||||
|
||||
|
|
40
src/lib.rs
40
src/lib.rs
|
@ -25,6 +25,7 @@
|
|||
use std::boxed::Box;
|
||||
use std::collections::HashMap;
|
||||
use std::net::{Shutdown, TcpStream};
|
||||
use std::time::Instant;
|
||||
use rayon::prelude::*;
|
||||
use reqwest::StatusCode;
|
||||
use reqwest::blocking::{Client, Response};
|
||||
|
@ -48,6 +49,7 @@ pub struct Resource {
|
|||
pub addr: String,
|
||||
pub custom: Option<HttpOptions>,
|
||||
pub kind: ResType,
|
||||
pub res: Option<String>,
|
||||
}
|
||||
|
||||
impl Resource {
|
||||
|
@ -68,11 +70,6 @@ impl Resource {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the description of the [`Resource`]
|
||||
pub fn description(&self) -> &String {
|
||||
&self.desc
|
||||
}
|
||||
|
||||
/// Checks an HTTP(s) endpoint's availability with a GET request.
|
||||
/// Prints a success message if the status code is 200 OK, or
|
||||
/// failure details in any other case.
|
||||
|
@ -93,7 +90,6 @@ impl Resource {
|
|||
/// or failure details when the status code is equaly to the `bad` value or
|
||||
/// any other value/error.
|
||||
fn check_http_custom(&self, options: &HttpOptions) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let client = Client::new();
|
||||
let resp: Response;
|
||||
if let Some(params) = &options.params {
|
||||
|
@ -115,7 +111,6 @@ impl Resource {
|
|||
/// is used.
|
||||
fn custom_http_resp(&self, options: &HttpOptions, resp: Response) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let resp_code = resp.status().as_u16();
|
||||
|
||||
if resp_code == options.ok {
|
||||
Ok(())
|
||||
} else {
|
||||
|
@ -153,13 +148,30 @@ pub struct NetworkResources {
|
|||
impl NetworkResources {
|
||||
/// Executes parallel connectivity checks for all [`Resource`]
|
||||
/// objects contained within the higher level [`NetworkResources`]
|
||||
/// struct.
|
||||
pub fn check_resources(self) {
|
||||
self.target.par_iter()
|
||||
.for_each(|el| match el.check() {
|
||||
Ok(_) => println!("Successfully connected to {}", el.description()),
|
||||
Err(e) => println!("Failed to connect to {} with: {}", el.description(), e)
|
||||
/// struct. Prints success message with call latency or failure message
|
||||
/// with available details. Maintains the resource order defined in the
|
||||
/// supplied TOML configuration file.
|
||||
pub fn check_resources(&mut self) {
|
||||
self.target.par_iter_mut()
|
||||
.for_each(|el| {
|
||||
let now = Instant::now();
|
||||
match el.check() {
|
||||
Ok(_) => {
|
||||
let dur = now.elapsed().as_millis();
|
||||
let res = format!("Successfully connected to {} in {}ms", el.desc, dur);
|
||||
el.res = Some(res);
|
||||
},
|
||||
Err(e) => {
|
||||
let res = format!("Failed to connect to {} with: {}", el.desc, e);
|
||||
el.res = Some(res);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
for target in self.target.iter() {
|
||||
if let Some(result) = &target.res {
|
||||
println!("{}", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue