Skip to main content

InstancePool

Struct InstancePool 

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

Pre-instantiated instance pool. Backed by a per-tuple crossbeam_channel of pre-spawned instances; see module docs for the channel layout, reset trade-off, and streaming-spawn carve-out.

Implementations§

Source§

impl InstancePool

Source

pub fn new(cfg: InstancePoolConfig) -> Self

Construct a new pool with the given config.

Source

pub async fn acquire( &self, executor: &TensorWasmExecutor, wasm: &[u8], cfg: SpawnConfig, ) -> Result<PooledInstance, ExecError>

Acquire a pre-spawned instance or fall through to a fresh spawn.

Algorithm (T37):

  1. Compute the (tenant, module_hash) key — building the entry on first observation by compiling the module (via the executor’s cached path), allocating a bounded channel of the configured size, and pre-spawning warm_instances_per_tuple detached instances into it.
  2. try_recv on the per-tuple channel. On hit, register the drawn instance with the executor and return a PooledInstance handle.
  3. On miss, fall through to TensorWasmExecutor::spawn_instance so the call still proceeds (admission control still applies — the executor’s max_instances cap fires here if saturated).

Streaming spawns (SpawnConfig::streaming set) bypass the warm-channel try_recv and always fall through to a fresh spawn: the streaming context is one-shot (the gateway-held receiver is drained per call), so the warm channel never holds streaming instances.

Source

pub async fn release( &self, executor: &TensorWasmExecutor, pooled: PooledInstance, cfg: &SpawnConfig, )

Release an instance back to the pool.

The instance is detached from the executor’s registry (the slot stays charged for the moment) and dropped — its linear memory could be holding sensitive guest state, so “reset” here means “re-instantiate”, never “rewind”. A fresh replacement is then spawned from the cached wasmtime::Module and try_send’d to the channel; on full / send-error / streaming carve-out / reset deadline exceeded, the replacement is dropped and the slot is released.

pooled carries the originating module hash so the release path dispatches to the correct per-tuple channel without re-hashing (the release path no longer has the wasm bytes on hand).

Source

pub fn warm_count(&self) -> usize

Number of currently warm (in-channel) instances across all tuples.

Source

pub fn draws_total(&self) -> usize

Total successful pool draws (hits + misses). Monotonic counter.

Source

pub fn hit_count(&self) -> usize

Draws that hit a warm instance in the channel. Monotonic counter.

Source

pub fn miss_count(&self) -> usize

Draws that fell through to a fresh spawn. Monotonic counter.

Source

pub fn drops_total(&self) -> usize

Number of pool drops (channel full, reset failure, streaming carve-out, or reset deadline exceeded). Monotonic counter.

Source

pub fn config(&self) -> &InstancePoolConfig

Borrow the pool’s configuration.

Source

pub fn shutdown(&self, executor: &TensorWasmExecutor)

Drain every warm channel and release its instances. Called by embedders that want to free pool-held wasmtime resources without dropping the executor (e.g. on a config reload).

Idempotent — calling on an already-empty pool is a no-op. Slot accounting on the executor is restored: every instance pulled out is dropped, and the corresponding slot is released.

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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