Skip to main content

WorkerState

Trait WorkerState 

Source
pub trait WorkerState:
    'static
    + Send
    + Sync {
    type StoreData: Send;
    type RequestId: Send + Sync;

    // Required methods
    fn should_accept_request(
        &self,
        concurrent_count: usize,
        total_count: usize,
    ) -> ShouldAccept;
    fn on_request_start(
        &self,
        store: StoreContextMut<'_, Self::StoreData>,
        id: Self::RequestId,
        task: GuestTaskId,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>;
    fn drop(&self, store: Store<Self::StoreData>, result: Result<(), Error>);
}
Available on crate feature component-model-async only.
Expand description

Represents the application-specific state of a ProxyHandler worker.

HandlerState::instantiate returns an implementation of this trait for each component instance (and thus each worker) created. The worker uses it to determine how many requests to accept, how long to wait for the guest to produce responses, etc.

Required Associated Types§

Source

type StoreData: Send

The type of the associated data for Store belonging to this worker.

Source

type RequestId: Send + Sync

An opaque unique identifier that hosts can assigned to requests which is threaded from ProxyHandler::handle into WorkerState::on_request_start

Required Methods§

Source

fn should_accept_request( &self, concurrent_count: usize, total_count: usize, ) -> ShouldAccept

Indicate whether the worker should accept another request given the current number it is already handling concurrently and the total it has handled so far.

Source

fn on_request_start( &self, store: StoreContextMut<'_, Self::StoreData>, id: Self::RequestId, task: GuestTaskId, ) -> Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>

Notification that a request has been accepted by the worker.

This method can be used to record anything within store, if necessary. The task corresponding to the component-model-level async task about to be created is additionally passed here.

If the future returned by this function resolves before the guest has produced a response, the request will be considered “expired” and the original ProxyHandler::handle future will resolve to an Err(ExpirationError.into()). In addition, the worker will stop accepting new requests but will continue running until all requests that have been accepted by the worker have either produced a response or expired, at which point the state of the worker will transition to either WorkerState::PostReturn or WorkerState::Idle.

Note that the returned future is polled from within the Store::run_concurrent event loop, and due to #11869 and #11870, it may not be polled at all for arbitrary lengths of time. Consequently, the Self::Expiration implementation (which is polled from outside the Store::run_concurrent event loop) must also enforce request expiration as a second level of defence if desired.

For example, if a request timeout of N seconds is to be enforced, the Self::Expiration::poll implementation, when called with WorkerState::Requests should calculate the time elapsed since the most recent outstanding request was accepted as indicated by the start parameter. If that time is greater than N seconds, we can expire the instance immediately, confident that all outstanding requests have expired.

Once #11869 and #11870 have been addressed, this “second level of defence” will no longer be necessary.

Source

fn drop(&self, store: Store<Self::StoreData>, result: Result<(), Error>)

Dispose of the store belonging to the now-exited worker.

This may be used to e.g. collect metrics from the store or its associated data before the store is dropped, as well as e.g. retry failed instantiations after the store is dropped.

If the store is being dropped due to an error (e.g. a guest trap or a host panic) result will be Err(_); otherwise it will be Ok(()).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§