Expand description
Robust, retrying wrapper around Alloy providers.
This module exposes RobustProvider, a small wrapper around Alloy’s
RootProvider that adds:
- bounded per-call timeouts
- exponential backoff retries
- transparent failover between a primary and one or more fallback providers
- more robust WebSocket block subscriptions with automatic reconnection
Use RobustProviderBuilder to construct a provider with sensible defaults
and optional fallbacks, or implement the IntoRobustProvider and IntoRootProvider
traits to support custom providers.
§How it works
All RPC calls performed through RobustProvider are wrapped in a total
timeout and retried with exponential backoff up to max_retries. If the
primary provider keeps failing, the call is retried against the configured
fallback providers in the order they were added. For subscriptions,
RobustSubscription also tracks lag, switches to fallbacks on repeated
failure, and periodically attempts to reconnect to the primary provider.
§Examples
Creating a robust WebSocket provider with a fallback:
use alloy::providers::{Provider, ProviderBuilder};
use robust_provider::RobustProviderBuilder;
use std::time::Duration;
use tokio_stream::StreamExt;
let ws = ProviderBuilder::new().connect("ws://localhost:8545").await?;
let ws_fallback = ProviderBuilder::new().connect("ws://localhost:8456").await?;
let robust = RobustProviderBuilder::new(ws)
.fallback(ws_fallback)
.call_timeout(Duration::from_secs(30))
.subscription_timeout(Duration::from_secs(120))
.build()
.await?;
// Make RPC calls with automatic retries and fallback
let block_number = robust.get_block_number().await?;
println!("Current block: {}", block_number);
// Create subscriptions that automatically reconnect on failure
let sub = robust.subscribe_blocks().await?;
let mut stream = sub.into_stream();
while let Some(response) = stream.next().await {
match response {
Ok(block) => println!("New block: {:?}", block),
Err(e) => println!("Got error: {:?}", e),
}
}You can also convert existing providers using IntoRobustProvider
Modules§
Macros§
- block_
not_ found_ doc - Documentation string for
BlockNotFounderrors, for use inrobust_rpc!macro calls.
Structs§
- Robust
Provider - Provider wrapper with built-in retry and timeout mechanisms.
- Robust
Provider Builder - Builder for constructing a
RobustProvider. - Robust
Subscription - A robust subscription wrapper that automatically handles provider failover and periodic reconnection attempts to the primary provider.
- Robust
Subscription Stream
Enums§
- Error
- Errors that can occur when using
super::RobustProvider. - Failover
Error - Low-level error related to RPC calls and failover logic.
Constants§
- DEFAULT_
CALL_ TIMEOUT - Default timeout used by
RobustProvider - DEFAULT_
MAX_ RETRIES - Default maximum number of retry attempts.
- DEFAULT_
MIN_ DELAY - Default base delay between retries.
- DEFAULT_
POLL_ INTERVAL - Default polling interval for HTTP subscriptions.
- DEFAULT_
RECONNECT_ INTERVAL - Default time interval between primary provider reconnection attempts
- DEFAULT_
SUBSCRIPTION_ BUFFER_ CAPACITY - Default subscription channel size.
- DEFAULT_
SUBSCRIPTION_ TIMEOUT - Default timeout for subscriptions
Traits§
- Into
Robust Provider - Conversion trait for types that can be turned into a
RobustProvider. - Into
Root Provider - Conversion trait for types that can be turned into an Alloy
RootProvider.