Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp: concurrent health check #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sqlx = { version = "0.6", features = [ "runtime-actix-native-tls" , "postgres",
deadpool-redis = "0.11.0"
chrono = "0.4.23"
uuid = "1.2.2"
tokio = "1.23.0"

[dev-dependencies]
serial_test = "0.9.0"
8 changes: 5 additions & 3 deletions src/components/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use super::{
configuration::Config, database::DatabaseComponent, health::HealthComponent,
redis::RedisComponent, synapse::SynapseComponent,
Expand Down Expand Up @@ -36,9 +38,9 @@ impl AppComponents {
panic!("Unable connecting to redis {:?}", err)
}

// TODO: Should we refactor HealthComponent to avoid cloning structs?
health.register_component(Box::new(db.clone()), "database".to_string());
health.register_component(Box::new(redis.clone()), "redis".to_string());
// TODO: Should we refactor HealthComponent to avoid cloning structs or should we refactor components for a cheapier cloning?
health.register_component(Arc::new(db.clone()), "database".to_string());
health.register_component(Arc::new(redis.clone()), "redis".to_string());

Self {
config,
Expand Down
25 changes: 18 additions & 7 deletions src/components/health.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt};
use std::{collections::HashMap, fmt, sync::Arc};

use async_trait::async_trait;

Expand All @@ -13,7 +13,7 @@ pub trait Healthy {
}

struct ComponentToCheck {
component: Box<dyn Healthy + Send + Sync>,
component: Arc<dyn Healthy + Send + Sync>,
name: String,
}

Expand All @@ -31,20 +31,32 @@ pub struct HealthComponent {
}

impl HealthComponent {
pub fn register_component(&mut self, component: Box<dyn Healthy + Send + Sync>, name: String) {
pub fn register_component(&mut self, component: Arc<dyn Healthy + Send + Sync>, name: String) {
self.components_to_check
.push(ComponentToCheck { component, name });
}

#[tracing::instrument(name = "Calculate components status")]
pub async fn calculate_status(&self) -> HashMap<String, ComponentHealthStatus> {
let mut result = HashMap::new();
let (tx, mut rx) = tokio::sync::mpsc::channel(self.components_to_check.len());

// TODO: Parallelize this checks
for component in self.components_to_check.as_slice() {
let healthy = component.component.is_healthy().await;
let tx_cloned = tx.clone();
let component_cloned = component.component.clone();
let component_name = component.name.clone();
log::debug!("About to check: {}", component_name);
tokio::spawn(async move {
let healthy = component_cloned.is_healthy().await;
tx_cloned.send((component_name, healthy)).await.unwrap()
});
log::debug!("Spawned: {}", component.name);
}
drop(tx);

while let Some((name, healthy)) = rx.recv().await {
result.insert(
component.name.to_string(),
name,
ComponentHealthStatus {
status: if healthy {
PASS.to_string()
Expand All @@ -54,7 +66,6 @@ impl HealthComponent {
},
);
}

result
}
}