From 4e7e920b593341d71fe5e66d3db30df4159daa0c Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 9 Feb 2024 10:14:59 +0000 Subject: [PATCH] feat: [#433] add timeout to health_check binary ``` cargo run --bin health_check http://127.0.0.1:3001/health_check ``` That command now fails if it does not get a response in 5 seconds. --- src/bin/health_check.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/bin/health_check.rs b/src/bin/health_check.rs index 16e2d874..1ac6652c 100644 --- a/src/bin/health_check.rs +++ b/src/bin/health_check.rs @@ -4,14 +4,17 @@ //! //! - They are harder to maintain. //! - They introduce new attack vectors. +use std::time::Duration; use std::{env, process}; +use reqwest::Client; + #[tokio::main] async fn main() { let args: Vec = env::args().collect(); if args.len() != 2 { eprintln!("Usage: cargo run --bin health_check "); - eprintln!("Example: cargo run --bin health_check http://localhost:3002/health_check"); + eprintln!("Example: cargo run --bin health_check http://127.0.0.1:3001/health_check"); std::process::exit(1); } @@ -19,7 +22,9 @@ async fn main() { let url = &args[1].clone(); - match reqwest::get(url).await { + let client = Client::builder().timeout(Duration::from_secs(5)).build().unwrap(); + + match client.get(url).send().await { Ok(response) => { if response.status().is_success() { println!("STATUS: {}", response.status());