-
Notifications
You must be signed in to change notification settings - Fork 8
feat: gateway health monitoring #610
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
Open
vladimirvolek
wants to merge
5
commits into
main
Choose a base branch
from
gateway-health
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37acd8e
feat: gateway health monitoring
vladimirvolek d407aa1
feat: gateway health monitoring - do not start gateway with errors
vladimirvolek 6512065
feat: gateway health monitoring - better logs
vladimirvolek 114e6fd
feat: gateway health monitoring - review
vladimirvolek 288f218
feat: gateway health monitoring - format
vladimirvolek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,95 @@ | ||
| use crate::blockfrost::BlockfrostAPI; | ||
| use crate::db::DB; | ||
| use std::sync::Arc; | ||
| use tokio::sync::{Mutex, Notify}; | ||
| use tokio::time::{self, Duration, Instant}; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct HealthStatus { | ||
| pub healthy: bool, | ||
| pub errors: Vec<String>, | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct HealthMonitor { | ||
| status: Arc<Mutex<HealthStatus>>, | ||
| } | ||
|
|
||
| impl HealthMonitor { | ||
| const DB_PING_TIMEOUT: Duration = Duration::from_secs(5); | ||
| const BLOCKFROST_PING_TIMEOUT: Duration = Duration::from_secs(5); | ||
| /// The Blockfrost check runs on its own, slower cadence to keep the | ||
| /// request-quota usage of the health checks negligible. | ||
| const BLOCKFROST_CHECK_INTERVAL: Duration = Duration::from_secs(60); | ||
| const HEALTHY_CHECK_INTERVAL: Duration = Duration::from_secs(10); | ||
| const UNHEALTHY_CHECK_INTERVAL: Duration = Duration::from_secs(2); | ||
|
|
||
| pub async fn current_status(&self) -> HealthStatus { | ||
| self.status.lock().await.clone() | ||
| } | ||
|
|
||
| pub fn new_static(status: HealthStatus) -> Self { | ||
| Self { | ||
| status: Arc::new(Mutex::new(status)), | ||
| } | ||
| } | ||
|
|
||
| pub async fn spawn(db: DB, blockfrost_api: BlockfrostAPI) -> Self { | ||
| let self_ = Self::new_static(HealthStatus { | ||
| healthy: true, | ||
| errors: vec![], | ||
| }); | ||
| let status = self_.status.clone(); | ||
|
|
||
| let first_check_done = Arc::new(Notify::new()); | ||
| let first_check_done_ = first_check_done.clone(); | ||
|
|
||
| tokio::spawn(async move { | ||
| let mut previously_healthy = true; | ||
| let mut blockfrost_error: Option<String> = None; | ||
| let mut last_blockfrost_check: Option<Instant> = None; | ||
| loop { | ||
| let db_error = db.ping(Self::DB_PING_TIMEOUT).await.err(); | ||
|
|
||
| if last_blockfrost_check | ||
| .is_none_or(|at| at.elapsed() >= Self::BLOCKFROST_CHECK_INTERVAL) | ||
| { | ||
| blockfrost_error = blockfrost_api | ||
| .ping(Self::BLOCKFROST_PING_TIMEOUT) | ||
| .await | ||
| .err(); | ||
| last_blockfrost_check = Some(Instant::now()); | ||
| } | ||
|
|
||
| let errors: Vec<String> = [db_error, blockfrost_error.clone()] | ||
| .into_iter() | ||
| .flatten() | ||
| .collect(); | ||
| let healthy = errors.is_empty(); | ||
|
|
||
| if previously_healthy && !healthy { | ||
| tracing::warn!("Gateway became unhealthy: {}", errors.join("; ")); | ||
| } else if !previously_healthy && healthy { | ||
| tracing::warn!("Gateway became healthy again."); | ||
| } | ||
|
|
||
| previously_healthy = healthy; | ||
|
|
||
| *status.lock().await = HealthStatus { healthy, errors }; | ||
|
|
||
| first_check_done_.notify_one(); | ||
|
|
||
| let delay = if healthy { | ||
| Self::HEALTHY_CHECK_INTERVAL | ||
| } else { | ||
| Self::UNHEALTHY_CHECK_INTERVAL | ||
| }; | ||
|
|
||
| time::sleep(delay).await; | ||
| } | ||
| }); | ||
|
|
||
| first_check_done.notified().await; | ||
| self_ | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.