Skip to main content

SessionPool

Struct SessionPool 

Source
pub struct SessionPool<'lean> { /* private fields */ }
Expand description

A capacity-bounded reuse pool of imported Lean environments.

Built with Self::with_capacity; environments enter the pool through PooledSession::drop (returning a previously-acquired session). Pool entries are capability-agnostic: a single pool may be shared across multiple LeanCapabilities values, as long as they share the same runtime.

Neither Send nor Sync (inherited from the contained Obj<'lean> values).

Implementations§

Source§

impl<'lean> SessionPool<'lean>

Source

pub fn with_capacity(runtime: &'lean LeanRuntime, capacity: usize) -> Self

Build an empty pool with hard upper bound capacity on stored environments.

A capacity of 0 disables reuse — every Self::acquire call imports fresh and every release drops the environment. This is useful for tests that want metrics without recycling, and as the degenerate point that proves the pool’s metrics agree with repeated caps.session(..., None, None) calls.

The runtime borrow witnesses 'lean and is stored so the pool itself outlives every entry on its free list — even after every PooledSession has been dropped, the pool retains a usable runtime reference.

Source

pub fn acquire<'p, 'c>( &'p self, caps: &'c LeanCapabilities<'lean, 'c>, imports: &[&str], cancellation: Option<&LeanCancellationToken>, progress: Option<&dyn LeanProgressSink>, ) -> LeanResult<PooledSession<'lean, 'p, 'c>>

Acquire a session targeting imports under caps.

If a pooled environment was previously released with the same imports list (order-sensitive), it is rewrapped under the supplied capability borrow and returned — no Lean.importModules runs. Otherwise the pool calls LeanCapabilities::session internally to perform a fresh import. Either way, the resulting PooledSession returns the underlying environment to the pool on Drop.

caps must come from the same LeanRuntime the pool was constructed with; this is structurally enforced by the shared 'lean lifetime parameter.

§Errors

Returns lean_rs::LeanError::Cancelled if cancellation is already cancelled before the pool can reuse or import an environment.

Returns lean_rs::LeanError::LeanException if a fresh import is required and the Lean-side lean_rs_host_session_import shim raises through IO. Cached environments never re-fail.

Source

pub fn stats(&self) -> PoolStats

Snapshot the accumulated pool metrics.

Counters never reset; subtract two snapshots to measure activity over an interval. See PoolStats for the field invariants (e.g. imports_performed + reused == acquired).

Source

pub fn len(&self) -> usize

Number of environments currently sitting on the free list.

This is the count of warm imports available for the next Self::acquire without going through Lean.importModules. Explicit drains and cache hits both remove entries from this count; releases may add entries back up to Self::capacity.

Source

pub fn is_empty(&self) -> bool

true iff Self::len is zero; every subsequent Self::acquire will perform a fresh import.

Source

pub fn capacity(&self) -> usize

Configured hard upper bound on the free list.

Set by Self::with_capacity. A pool releasing a PooledSession while at capacity drops the environment instead of pushing it back; that release shows up in PoolStats::released_dropped rather than released_to_pool.

Source

pub fn drain(&self) -> usize

Drop every cached environment currently retained by the pool.

Returns the number of free-list entries removed. Each removed entry drops its owned Obj<'lean> environment, which releases one Lean refcount via lean_dec.

Checked-out PooledSession values are not affected: they own their sessions until drop, and may return their environments to this same pool later if capacity permits. A later Self::drain call can remove those returned entries.

This is a cache-eviction API, not a runtime recycle API. It does not reset Lean’s process-global runtime state, initialized module flags, interned names, compacted .olean regions, or allocator arenas, and should not be treated as an RSS reset.

Trait Implementations§

Source§

impl Debug for SessionPool<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'lean> !Freeze for SessionPool<'lean>

§

impl<'lean> !RefUnwindSafe for SessionPool<'lean>

§

impl<'lean> !Send for SessionPool<'lean>

§

impl<'lean> !Sync for SessionPool<'lean>

§

impl<'lean> Unpin for SessionPool<'lean>

§

impl<'lean> UnsafeUnpin for SessionPool<'lean>

§

impl<'lean> UnwindSafe for SessionPool<'lean>

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