Skip to main content

ClientPool

Struct ClientPool 

Source
pub struct ClientPool { /* private fields */ }
Expand description

Single-threaded async HTTP client pool.

Pre-allocated slots with LIFO acquire for cache locality. Each slot owns a RequestWriter, ResponseReader, and HttpConnection.

§Usage

let pool = ClientPool::builder()
    .url("https://api.binance.com")
    .base_path("/api/v3")
    .default_header("X-API-KEY", &key)?
    .connections(4)
    .tls(&tls)
    .build()
    .await?;

// Fast path (trading) — no reconnect, no wait
let mut slot = pool.try_acquire().unwrap();
// Patient path (background) — waits, reconnects with backoff
let mut slot = pool.acquire().await?;

let s: &mut ClientSlot = &mut slot;
let req = s.writer.post("/order").body(json).finish()?;
let conn = s.conn.as_mut().unwrap();
let resp = conn.send(req, &mut s.reader).await?;
// drop(slot) returns to pool

Implementations§

Source§

impl ClientPool

Source

pub fn builder() -> ClientPoolBuilder

Create a builder.

Source

pub fn try_acquire(&self) -> Option<Pooled<ClientSlot>>

Try to acquire a healthy client slot (LIFO).

Checks available slots for a healthy connection. Dead slots are ejected from the pool and a reconnect task is spawned for each. When reconnection succeeds, the slot returns to the pool automatically.

Returns None if all slots are in use or currently reconnecting.

This is the trading hot path — O(1) when the top slot is healthy.

Source

pub async fn acquire(&self) -> Result<Pooled<ClientSlot>, RestError>

Acquire a client slot, waiting until one is available.

If no healthy slots are available, waits for reconnect tasks to finish healing dead connections. Returns error if no slot becomes available within the retry limit.

This is the off-hot-path API for background tasks and REST calls.

Source

pub fn available(&self) -> usize

Number of slots currently available (not acquired).

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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