Skip to content

Commit

Permalink
Merge torrust#477: Add timeout to health_check binary
Browse files Browse the repository at this point in the history
4e7e920 feat: [torrust#433] add timeout to health_check binary (Jose Celano)

Pull request description:

  Add timeout to `health_check` binary.

  ```console
  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.

ACKs for top commit:
  josecelano:
    ACK 4e7e920

Tree-SHA512: 18cbc2abc12c83c20f2c8ea8acf0190755e14efc2226b1d63bf941ddc8d346bcb14018fe2361dfd5c7762f48514cf02745dbf04ae2968016b429f40f75af1034
  • Loading branch information
josecelano committed Feb 9, 2024
2 parents 7c3f798 + 4e7e920 commit f73dcfa
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/bin/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
//!
//! - 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<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: cargo run --bin health_check <HEALTH_URL>");
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);
}

println!("Health check ...");

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());
Expand Down

0 comments on commit f73dcfa

Please sign in to comment.