-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): Added http healthcheck endpoint (#1725)
- Loading branch information
Showing
26 changed files
with
862 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use health::HealthCheck; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub struct VMPoolHealth { | ||
expected_count: usize, | ||
current_count: Arc<AtomicUsize>, | ||
} | ||
|
||
impl VMPoolHealth { | ||
pub fn new(expected_count: usize) -> Self { | ||
Self { | ||
expected_count, | ||
current_count: Arc::new(AtomicUsize::new(0)), | ||
} | ||
} | ||
|
||
pub fn increment_count(&self) { | ||
self.current_count.fetch_add(1, Ordering::Release); | ||
} | ||
} | ||
|
||
impl HealthCheck for VMPoolHealth { | ||
fn status(&self) -> eyre::Result<()> { | ||
let current = self.current_count.load(Ordering::Acquire); | ||
if self.expected_count != current { | ||
return Err(eyre::eyre!( | ||
"VM pool isn't full. Current: {}, Expected: {}", | ||
current, | ||
self.expected_count | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::thread; | ||
|
||
#[test] | ||
fn test_vm_pool_health_empty() { | ||
let pool_health = VMPoolHealth::new(0); | ||
let status = pool_health.status(); | ||
assert!(status.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_vm_pool_health_partial() { | ||
let pool_health = VMPoolHealth::new(5); | ||
pool_health.increment_count(); | ||
pool_health.increment_count(); | ||
pool_health.increment_count(); | ||
|
||
let status = pool_health.status(); | ||
assert!(status.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_vm_pool_health_full() { | ||
let pool_health = VMPoolHealth::new(3); | ||
pool_health.increment_count(); | ||
pool_health.increment_count(); | ||
pool_health.increment_count(); | ||
|
||
let status = pool_health.status(); | ||
assert!(status.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_vm_pool_health_concurrent_access() { | ||
let pool_health = VMPoolHealth::new(100); | ||
let mut handles = vec![]; | ||
|
||
// Simulate concurrent access by spawning multiple threads. | ||
for _ in 0..50 { | ||
let pool_health_clone = pool_health.clone(); | ||
let handle = thread::spawn(move || { | ||
for _ in 0..2 { | ||
pool_health_clone.increment_count(); | ||
} | ||
}); | ||
handles.push(handle); | ||
} | ||
|
||
// Wait for all threads to finish. | ||
for handle in handles { | ||
handle.join().unwrap(); | ||
} | ||
|
||
let status = pool_health.status(); | ||
assert!(status.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "health" | ||
version = "0.1.0" | ||
authors = ["Fluence Labs"] | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
eyre = { workspace = true } |
Oops, something went wrong.