pub struct ProviderPool { /* private fields */ }Expand description
A thread-safe pool of providers indexed by chain
The pool uses read-write locks for efficient concurrent access:
- Multiple readers can access providers simultaneously
- Writers acquire exclusive access when adding new providers
§Thread Safety
The pool is safe to share across threads via Arc<ProviderPool> or
by storing it in a static variable with LazyLock.
Implementations§
Source§impl ProviderPool
impl ProviderPool
Sourcepub fn with_defaults(rate_limit: Option<u32>) -> Self
pub fn with_defaults(rate_limit: Option<u32>) -> Self
Create a pool with default rate limit for new providers
Sourcepub fn with_default_timeout(self, timeout: Duration) -> Self
pub fn with_default_timeout(self, timeout: Duration) -> Self
Set the default per-request timeout used when an endpoint does not override its own.
Sourcepub fn from_endpoints(
endpoints: Vec<ChainEndpoint>,
rate_limit: Option<u32>,
) -> Result<Self, RpcError>
pub fn from_endpoints( endpoints: Vec<ChainEndpoint>, rate_limit: Option<u32>, ) -> Result<Self, RpcError>
Create a pool from a list of chain endpoints
§Errors
Returns an error if any endpoint URL is invalid
Sourcepub fn add(
&self,
chain: NamedChain,
url: &str,
rate_limit: Option<u32>,
) -> Result<(), RpcError>
pub fn add( &self, chain: NamedChain, url: &str, rate_limit: Option<u32>, ) -> Result<(), RpcError>
Add a provider for a specific chain
If a provider already exists for this chain, it will be replaced.
§Arguments
chain- The chain to add the provider forurl- The RPC endpoint URLrate_limit- Optional rate limit in requests per second
The per-request timeout is taken from the pool’s default
(see with_default_timeout).
Use add_endpoint to set a per-chain timeout
or minimum-delay pacing.
§Errors
Returns an error if the URL is invalid
Sourcepub fn add_endpoint(&self, endpoint: &ChainEndpoint) -> Result<(), RpcError>
pub fn add_endpoint(&self, endpoint: &ChainEndpoint) -> Result<(), RpcError>
Add a chain endpoint to the pool.
Uses the endpoint’s rate_limit, timeout, and min_delay when
set, otherwise falls back to the pool defaults.
§Errors
Returns an error if the endpoint URL is invalid.
Sourcepub fn get(&self, chain: NamedChain) -> Option<PooledProvider>
pub fn get(&self, chain: NamedChain) -> Option<PooledProvider>
Get a provider for a specific chain
Returns None if no provider is configured for the chain.
Sourcepub fn get_or_add(
&self,
chain: NamedChain,
url: &str,
rate_limit: Option<u32>,
) -> Result<PooledProvider, RpcError>
pub fn get_or_add( &self, chain: NamedChain, url: &str, rate_limit: Option<u32>, ) -> Result<PooledProvider, RpcError>
Get a provider for a chain, or add it if not present
This is useful for lazy initialization of providers.
§Errors
Returns an error if the URL is invalid and the provider needs to be created
Sourcepub fn remove(&self, chain: NamedChain) -> Option<PooledProvider>
pub fn remove(&self, chain: NamedChain) -> Option<PooledProvider>
Remove a provider from the pool
Returns the removed provider if it existed.
Sourcepub fn contains(&self, chain: NamedChain) -> bool
pub fn contains(&self, chain: NamedChain) -> bool
Check if a provider exists for a chain
Sourcepub fn chains(&self) -> Vec<NamedChain>
pub fn chains(&self) -> Vec<NamedChain>
Get all configured chains
Trait Implementations§
Source§impl Debug for ProviderPool
impl Debug for ProviderPool
Source§impl Default for ProviderPool
impl Default for ProviderPool
Source§fn default() -> ProviderPool
fn default() -> ProviderPool
Auto Trait Implementations§
impl !Freeze for ProviderPool
impl RefUnwindSafe for ProviderPool
impl Send for ProviderPool
impl Sync for ProviderPool
impl Unpin for ProviderPool
impl UnsafeUnpin for ProviderPool
impl UnwindSafe for ProviderPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more