Skip to main content

ProviderPool

Struct ProviderPool 

Source
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

Source

pub fn new() -> Self

Create a new empty provider pool

Source

pub fn with_defaults(rate_limit: Option<u32>) -> Self

Create a pool with default rate limit for new providers

Source

pub fn with_default_timeout(self, timeout: Duration) -> Self

Set the default per-request timeout used when an endpoint does not override its own.

Source

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

Source

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 for
  • url - The RPC endpoint URL
  • rate_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

Source

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.

Source

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.

Source

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

Source

pub fn remove(&self, chain: NamedChain) -> Option<PooledProvider>

Remove a provider from the pool

Returns the removed provider if it existed.

Source

pub fn contains(&self, chain: NamedChain) -> bool

Check if a provider exists for a chain

Source

pub fn len(&self) -> usize

Get the number of providers in the pool

Source

pub fn is_empty(&self) -> bool

Check if the pool is empty

Source

pub fn chains(&self) -> Vec<NamedChain>

Get all configured chains

Source

pub fn clear(&self)

Clear all providers from the pool

Trait Implementations§

Source§

impl Debug for ProviderPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ProviderPool

Source§

fn default() -> ProviderPool

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more