Skip to content
Merged
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
19 changes: 8 additions & 11 deletions src/robust_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use alloy::{
transports::{RpcError, TransportErrorKind},
};
use backon::{ExponentialBuilder, Retryable};
use futures::TryFutureExt;
use thiserror::Error;
use tokio::time::{error as TokioError, timeout};
use tracing::{error, info};
Expand All @@ -28,7 +29,7 @@ pub enum Error {

/// Low-level error related to RPC calls and failover logic.
#[derive(Error, Debug)]
pub(crate) enum CoreError {
pub enum CoreError {
#[error("Operation timed out")]
Timeout,
#[error("RPC call failed after exhausting all retry attempts: {0}")]
Expand Down Expand Up @@ -315,7 +316,7 @@ impl<N: Network> RobustProvider<N> {
/// * Returns [`RpcError::Transport(TransportErrorKind::PubsubUnavailable)`] if `require_pubsub`
/// is true and all providers don't support pubsub.
/// * Propagates any [`RpcError<TransportErrorKind>`] from the underlying retries.
pub(crate) async fn try_operation_with_failover<T: Debug, F, Fut>(
pub async fn try_operation_with_failover<T: Debug, F, Fut>(
&self,
operation: F,
require_pubsub: bool,
Expand All @@ -325,15 +326,11 @@ impl<N: Network> RobustProvider<N> {
Fut: Future<Output = Result<T, RpcError<TransportErrorKind>>>,
{
let primary = self.primary();
let result = self.try_provider_with_timeout(primary, &operation).await;

if result.is_ok() {
return result;
}

let last_error = result.unwrap_err();

self.try_fallback_providers(&operation, require_pubsub, last_error).await
self.try_provider_with_timeout(primary, &operation)
.or_else(|last_error| {
self.try_fallback_providers(&operation, require_pubsub, last_error)
})
.await
}

pub(crate) async fn try_fallback_providers<T: Debug, F, Fut>(
Expand Down