hedged_rpc_client/
lib.rs

1//! A Solana RPC client that implements hedged requests across multiple providers.
2//!
3//! This library races RPC requests to multiple endpoints and returns the first successful
4//! response, significantly reducing tail latency while maintaining reliability.
5//!
6//! # Quick Start
7//!
8//! ```no_run
9//! use hedged_rpc_client::{HedgedRpcClient, HedgeConfig, ProviderConfig, ProviderId};
10//! use std::time::Duration;
11//!
12//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
13//! let providers = vec![
14//!     ProviderConfig {
15//!         id: ProviderId("helius"),
16//!         url: "https://mainnet.helius-rpc.com".to_string(),
17//!     },
18//!     ProviderConfig {
19//!         id: ProviderId("triton"),
20//!         url: "https://triton.helius.xyz".to_string(),
21//!     },
22//! ];
23//!
24//! let config = HedgeConfig::low_latency(providers.len());
25//! let client = HedgedRpcClient::new(providers, config);
26//!
27//! let (provider, blockhash) = client.get_latest_blockhash().await?;
28//! println!("Got blockhash from {}: {}", provider.0, blockhash);
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! # Hedging Strategy
34//!
35//! The client uses a configurable hedging strategy:
36//! 1. Initially queries `initial_providers` endpoints
37//! 2. If no response after `hedge_after` duration, fans out to more providers
38//! 3. Returns the first successful response
39//! 4. Times out after `overall_timeout` if all providers fail
40//!
41//! # Preset Configurations
42//!
43//! Use `HedgeConfig::low_latency()`, `::conservative()`, or `::aggressive()` for
44//! common hedging strategies, or create a custom configuration.
45
46pub mod client;
47pub mod config;
48pub mod errors;
49
50pub use client::{HedgedRpcClient, ProviderStatsSnapshot};
51pub use config::{HedgeConfig, ProviderConfig, ProviderId};
52pub use errors::HedgedError;
53pub use solana_sdk::{hash::Hash, pubkey::Pubkey};