Skip to content

Commit

Permalink
refactor: [#508] extract health check methods
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 24, 2023
1 parent bf23479 commit 5e0a686
Showing 1 changed file with 44 additions and 14 deletions.
58 changes: 44 additions & 14 deletions src/servers/health_check_api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;
use aquatic_udp_protocol::{ConnectRequest, Response, TransactionId};
use axum::extract::State;
use axum::Json;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, UdpTracker};

use super::resources::Report;
use super::responses;
Expand All @@ -21,27 +21,49 @@ const UNKNOWN_PORT: u16 = 0;
/// configuration. If port 0 is specified in the configuration the health check
/// for that service is skipped.
pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>>) -> Json<Report> {
if let Some(err_response) = api_health_check(&config.http_api).await {
return err_response;

Check warning on line 25 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L25

Added line #L25 was not covered by tests
}

if let Some(err_response) = http_trackers_health_check(&config.http_trackers).await {
return err_response;

Check warning on line 29 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L29

Added line #L29 was not covered by tests
}

if let Some(err_response) = udp_trackers_health_check(&config.udp_trackers).await {
return err_response;

Check warning on line 33 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L33

Added line #L33 was not covered by tests
}

responses::ok()
}

async fn api_health_check(config: &HttpApi) -> Option<Json<Report>> {
// todo: when port 0 is specified in the configuration get the port from the
// running service, after starting it as we do for testing with ephemeral
// configurations.

// Health check for API

if config.http_api.enabled {
let addr: SocketAddr = config.http_api.bind_address.parse().expect("invalid socket address for API");
if config.enabled {
let addr: SocketAddr = config.bind_address.parse().expect("invalid socket address for API");

Check warning on line 45 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L45

Added line #L45 was not covered by tests

if addr.port() != UNKNOWN_PORT {
let health_check_url = format!("http://{addr}/health_check");

Check warning on line 48 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L47-L48

Added lines #L47 - L48 were not covered by tests

if !get_req_is_ok(&health_check_url).await {
return responses::error(format!("API is not healthy. Health check endpoint: {health_check_url}"));
return Some(responses::error(format!(

Check warning on line 51 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L50-L51

Added lines #L50 - L51 were not covered by tests
"API is not healthy. Health check endpoint: {health_check_url}"
)));

Check warning on line 53 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L53

Added line #L53 was not covered by tests
}
}

Check warning on line 55 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L55

Added line #L55 was not covered by tests
}

// Health check for HTTP Trackers
None
}

for http_tracker_config in &config.http_trackers {
async fn http_trackers_health_check(http_trackers: &Vec<HttpTracker>) -> Option<Json<Report>> {
// todo: when port 0 is specified in the configuration get the port from the
// running service, after starting it as we do for testing with ephemeral
// configurations.

for http_tracker_config in http_trackers {
if !http_tracker_config.enabled {
continue;
}
Expand All @@ -55,16 +77,22 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
let health_check_url = format!("http://{addr}/health_check");

Check warning on line 77 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L76-L77

Added lines #L76 - L77 were not covered by tests

if !get_req_is_ok(&health_check_url).await {
return responses::error(format!(
return Some(responses::error(format!(

Check warning on line 80 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L79-L80

Added lines #L79 - L80 were not covered by tests
"HTTP Tracker is not healthy. Health check endpoint: {health_check_url}"
));
)));

Check warning on line 82 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L82

Added line #L82 was not covered by tests
}
}

Check warning on line 84 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L84

Added line #L84 was not covered by tests
}

// Health check for UDP Trackers
None
}

async fn udp_trackers_health_check(udp_trackers: &Vec<UdpTracker>) -> Option<Json<Report>> {
// todo: when port 0 is specified in the configuration get the port from the
// running service, after starting it as we do for testing with ephemeral
// configurations.

for udp_tracker_config in &config.udp_trackers {
for udp_tracker_config in udp_trackers {
if !udp_tracker_config.enabled {
continue;
}
Expand All @@ -75,11 +103,13 @@ pub(crate) async fn health_check_handler(State(config): State<Arc<Configuration>
.expect("invalid socket address for UDP tracker");

Check warning on line 103 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L103

Added line #L103 was not covered by tests

if addr.port() != UNKNOWN_PORT && !can_connect_to_udp_tracker(&addr.to_string()).await {
return responses::error(format!("UDP Tracker is not healthy. Can't connect to: {addr}"));
return Some(responses::error(format!(

Check warning on line 106 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L105-L106

Added lines #L105 - L106 were not covered by tests
"UDP Tracker is not healthy. Can't connect to: {addr}"
)));

Check warning on line 108 in src/servers/health_check_api/handlers.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/health_check_api/handlers.rs#L108

Added line #L108 was not covered by tests
}
}

responses::ok()
None
}

async fn get_req_is_ok(url: &str) -> bool {
Expand Down

0 comments on commit 5e0a686

Please sign in to comment.