Skip to content

Add timeout to the http_health_check.rs binary #604

@josecelano

Description

@josecelano

Parent issue: #603
Relates to: #597

The healthcheck binary we use for docker HEALTHCHECKs needs a timeout.

It's producing problems when the service does not respond.

#[tokio::main]
async fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        eprintln!("Usage:   cargo run --bin http_health_check <HEALTH_URL>");
        eprintln!("Example: cargo run --bin http_health_check http://127.0.0.1:1313/health_check");
        std::process::exit(1);
    }

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

    let url = &args[1].clone();

    match reqwest::get(url).await {
        Ok(response) => {
            if response.status().is_success() {
                println!("STATUS: {}", response.status());
                process::exit(0);
            } else {
                println!("Non-success status received.");
                process::exit(1);
            }
        }
        Err(err) => {
            println!("ERROR: {err}");
            process::exit(1);
        }
    }
}

It could be something like:

use std::{env, process, time::Duration};
use reqwest::Client;

#[tokio::main]
async fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        eprintln!("Usage:   cargo run --bin http_health_check <HEALTH_URL>");
        eprintln!("Example: cargo run --bin http_health_check http://127.0.0.1:1313/health_check");
        process::exit(1);
    }

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

    let url = &args[1];

    let client = Client::builder()
        .timeout(Duration::from_secs(5)) // Timeout is set to 5 seconds
        .build()
        .unwrap();

    match client.get(url).send().await {
        Ok(response) => {
            if response.status().is_success() {
                println!("STATUS: {}", response.status());
                process::exit(0);
            } else {
                println!("Non-success status received.");
                process::exit(1);
            }
        }
        Err(err) => {
            println!("ERROR: {err}");
            process::exit(1);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIncorrect Behavior

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions