Skip to main content

BrainPool

Struct BrainPool 

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

A bounded connection-pool cache to Brain — per-credential (self-host) or a single shared service-principal pool with per-request act_as (gateway).

In per-credential mode the cache is keyed by the wire credential string and held in an ArcSwap so the hot path (pool already built) is a lock-free load + map lookup with no per-request hashing beyond the lookup itself. In shared mode there is one pool for every caller. Pools are created lazily, on first use.

Implementations§

Source§

impl BrainPool

Source

pub fn new(config: BrainPoolConfig) -> Self

Build an empty per-credential pool cache (the self-host bearer default). Pools are created lazily, on first use per credential. max_credentials is floored at 1 so the cache can always hold at least one entry.

Source

pub fn shared(config: BrainPoolConfig, service_credential: String) -> Self

Build a shared / act_as pool authenticated as service_credential — a trusted service principal holding the can_act_as grant. Every BrainPool::client_for call returns a client from this ONE pool regardless of the credential argument, so a single connection pool can serve every tenant when the gateway sets act_as per request.

The pool is opened lazily on first use, so this constructor is synchronous and infallible just like BrainPool::new; a bad service credential or an unreachable Brain surfaces from the first client_for.

Source

pub async fn client_for( &self, credential: &str, ) -> Result<Arc<BrainClient>, BrainError>

Get (or lazily build) a pooled client.

In per-credential mode the client is authenticated as credential: hot path is a lock-free load + lookup, bumping recency in place with a relaxed atomic; cold path builds the brain_db_sdk::Pool OUTSIDE any lock, publishes it via ArcSwap::rcu, and LRU-evicts the least-recently-used entry first if the cache is at max_credentials.

In shared mode credential is ignored: the returned client always comes from the single service-principal pool. The gateway then sets act_as per request to select the effective tenant identity.

§Errors

Returns the underlying BrainError if a fresh pool fails to connect or handshake.

Source

pub fn sweep_idle(&self)

Drop credentials idle beyond config.idle_ttl. Called by a background sweeper, never from the request path. One RCU pass; a no-op if nothing has gone idle. In shared mode there is a single long-lived pool with no per-credential entries to sweep, so this is a no-op.

Source

pub async fn probe(&self, timeout: Duration) -> bool

Cheap reachability probe used by readiness: can we open a TCP socket to the Brain listener within timeout? Avoids a full handshake or a credential.

Trait Implementations§

Source§

impl Debug for BrainPool

Source§

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

Reports the mode and the cache ceiling — never a credential, and never the pooled connections themselves.

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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