mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-07 03:22:20 +00:00
99737c551a
## Issue Addressed Resolves #2487 ## Proposed Changes Logs a message once in every invocation of `Eth1Service::update` method if the primary endpoint is unavailable for some reason. e.g. ```log Aug 03 00:09:53.517 WARN Error connecting to eth1 node endpoint action: trying fallbacks, endpoint: http://localhost:8545/, service: eth1_rpc Aug 03 00:09:56.959 INFO Fetched data from fallback fallback_number: 1, service: eth1_rpc ``` The main aim of this PR is to have an accompanying message to the "action: trying fallbacks" error message that is returned when checking the endpoint for liveness. This is mainly to indicate to the user that the fallback was live and reachable. ## Additional info This PR is not meant to be a catch all for all cases where the primary endpoint failed. For instance, this won't log anything if the primary node was working fine during endpoint liveness checking and failed during deposit/block fetching. This is done intentionally to reduce number of logs while initial deposit/block sync and to avoid more complicated logic.
64 lines
1.7 KiB
Rust
64 lines
1.7 KiB
Rust
use itertools::{join, zip};
|
|
use std::fmt::{Debug, Display};
|
|
use std::future::Future;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Fallback<T> {
|
|
pub servers: Vec<T>,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum FallbackError<E> {
|
|
AllErrored(Vec<E>),
|
|
}
|
|
|
|
impl<T> Fallback<T> {
|
|
pub fn new(servers: Vec<T>) -> Self {
|
|
Self { servers }
|
|
}
|
|
|
|
/// Return the first successful result along with number of previous errors encountered
|
|
/// or all the errors encountered if every server fails.
|
|
pub async fn first_success<'a, F, O, E, R>(
|
|
&'a self,
|
|
func: F,
|
|
) -> Result<(O, usize), FallbackError<E>>
|
|
where
|
|
F: Fn(&'a T) -> R,
|
|
R: Future<Output = Result<O, E>>,
|
|
{
|
|
let mut errors = vec![];
|
|
for server in &self.servers {
|
|
match func(server).await {
|
|
Ok(val) => return Ok((val, errors.len())),
|
|
Err(e) => errors.push(e),
|
|
}
|
|
}
|
|
Err(FallbackError::AllErrored(errors))
|
|
}
|
|
|
|
pub fn map_format_error<'a, E, F, S>(&'a self, f: F, error: &FallbackError<E>) -> String
|
|
where
|
|
F: FnMut(&'a T) -> &'a S,
|
|
S: Display + 'a,
|
|
E: Debug,
|
|
{
|
|
match error {
|
|
FallbackError::AllErrored(v) => format!(
|
|
"All fallback errored: {}",
|
|
join(
|
|
zip(self.servers.iter().map(f), v.iter())
|
|
.map(|(server, error)| format!("{} => {:?}", server, error)),
|
|
", "
|
|
)
|
|
),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: Display> Fallback<T> {
|
|
pub fn format_error<E: Debug>(&self, error: &FallbackError<E>) -> String {
|
|
self.map_format_error(|s| s, error)
|
|
}
|
|
}
|