Struct JsWorker
pub struct JsWorker { /* private fields */ }js and std only.Expand description
A JsRuntime owned by a dedicated OS thread: the
compile-once, call-many execution model.
State (globals, function definitions, …) persists for the lifetime
of the worker: execute a script once, then call into
its functions per request. This is the model browsers and proxies use
for long-lived configuration scripts; spawn a fresh worker instead
when a script must not observe earlier executions.
The handle is cheap to clone; all handles share the same runtime and jobs run strictly in order. The thread exits once the last handle is dropped.
A job that exceeds the worker’s timeout
releases its caller with a JsErrorKind::Timeout. A job still waiting
in the queue is cancelled and never runs. A job already running cannot be
interrupted when it is inside an arbitrary Rust host callback; until that
exact job finishes the worker is abandoned, and
later jobs fail fast with JsErrorKind::Setup instead of queueing behind
work that may never end. If it never returns, the worker is retired and
its thread remains occupied, so spawn a replacement. Without a timeout
configured, such a job blocks its caller and the queue indefinitely.
A panicking host function kills the worker: the pending and all later
jobs fail fast with a JsErrorKind::Setup error (fail-loud, rather
than continuing on a runtime in unknown state).
Implementations§
§impl JsWorker
impl JsWorker
pub const DEFAULT_QUEUE_CAPACITY: usize = 128
pub const DEFAULT_QUEUE_CAPACITY: usize = 128
Default capacity of the worker’s job queue.
pub fn builder() -> JsWorkerBuilder
pub fn builder() -> JsWorkerBuilder
Create a JsWorkerBuilder to configure a new worker.
pub fn spawn(builder: JsRuntimeBuilder) -> Result<JsWorker, JsError>
pub fn spawn(builder: JsRuntimeBuilder) -> Result<JsWorker, JsError>
Spawn a worker thread owning a fresh JsRuntime built from
the given builder, with the default worker configuration.
pub fn is_abandoned(&self) -> bool
pub fn is_abandoned(&self) -> bool
Whether the worker is stuck on a job that overran its timeout.
Abandoned workers refuse further jobs. The state clears by itself if the overrunning job does eventually finish; a job that never returns retires the worker for good, so spawn a replacement.
pub async fn run<T, F>(&self, f: F) -> Result<T, JsError>
pub async fn run<T, F>(&self, f: F) -> Result<T, JsError>
Execute the given closure with exclusive access to the worker’s runtime.
Dropping this future cancels the job while it is still queued. Once the job is running it is allowed to finish, since arbitrary Rust host code cannot be interrupted safely.
pub async fn eval<S>(&self, src: S) -> Result<JsValue, JsError>
pub async fn eval<S>(&self, src: S) -> Result<JsValue, JsError>
Evaluate a script on the worker’s runtime, returning the value of its final expression.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for JsWorker
impl !UnwindSafe for JsWorker
impl Freeze for JsWorker
impl Send for JsWorker
impl Sync for JsWorker
impl Unpin for JsWorker
impl UnsafeUnpin for JsWorker
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more