pub struct MainRuntime(/* private fields */);Expand description
The runtime the app runs on, as a handler reaches it: Ctx(main): Ctx<MainRuntime> as a
parameter, or Context::main_runtime.
A handler on dedicated threads (threads(n)) runs on its own thread, and so does everything
it leaves behind: a plain tokio::spawn, a timer, an and_after continuation, an after(..)
hook and the timer of a retry_after copy. MainRuntime is the one explicit way to send work
to the app’s runtime instead: spawn starts it there and moves on,
run runs it there and hands its output back. What is sent must be Send.
A plain tokio::spawn on a dedicated thread lives as long as that thread: a task still
pending when the subscription ends is dropped with the thread’s runtime.
On any other placement the handler already runs on this runtime.
§Examples
use ruststream::prelude::*;
#[derive(serde::Deserialize)]
struct Job {
id: u64,
}
async fn notify(_id: u64) {}
async fn lookup(id: u64) -> u64 {
id * 2
}
#[subscriber("jobs", threads(4))]
async fn index(job: &Job, Ctx(main): Ctx<MainRuntime>) -> HandlerOutcome {
// The notification leaves the thread; the handler does not wait for it.
main.spawn(notify(job.id));
// The lookup runs on the app's runtime, and its answer comes back here.
match main.run(lookup(job.id)).await {
Ok(_answer) => HandlerOutcome::ack(),
Err(_) => HandlerOutcome::retry(),
}
}Implementations§
Source§impl MainRuntime
impl MainRuntime
Sourcepub fn spawn<Work>(&self, future: Work) -> JoinHandle<<Work as Future>::Output> ⓘ
pub fn spawn<Work>(&self, future: Work) -> JoinHandle<<Work as Future>::Output> ⓘ
Starts future as a task of the app’s runtime and returns its handle; the handler does
not wait for it unless it awaits the handle.
§Examples
use ruststream::IncomingMessage;
use ruststream::runtime::{Context, HandlerOutcome};
async fn notify(_id: u64) {}
async fn handle<M: IncomingMessage>(_msg: &M, ctx: &mut Context<'_>) -> HandlerOutcome {
ctx.main_runtime().spawn(notify(7));
HandlerOutcome::ack()
}Sourcepub fn run<Work>(
&self,
future: Work,
) -> impl Future<Output = Result<<Work as Future>::Output, JoinError>> + Send + 'static
pub fn run<Work>( &self, future: Work, ) -> impl Future<Output = Result<<Work as Future>::Output, JoinError>> + Send + 'static
Runs future as a task of the app’s runtime and resolves to its output.
§Errors
Returns the JoinError of a task that panicked, or that the runtime cancelled because
it is shutting down.
§Cancel safety
Dropping the returned future stops waiting, not the work: the task runs to its end on the app’s runtime.
§Examples
use ruststream::IncomingMessage;
use ruststream::runtime::{Context, HandlerOutcome};
async fn lookup(id: u64) -> u64 {
id * 2
}
async fn handle<M: IncomingMessage>(_msg: &M, ctx: &mut Context<'_>) -> HandlerOutcome {
match ctx.main_runtime().run(lookup(7)).await {
Ok(14) => HandlerOutcome::ack(),
_ => HandlerOutcome::retry(),
}
}Sourcepub const fn as_handle(&self) -> &Handle
pub const fn as_handle(&self) -> &Handle
The Tokio handle of the app’s runtime, for what the two methods above do not cover
(block_on from a thread outside any runtime, the runtime’s metrics).
§Examples
use ruststream::IncomingMessage;
use ruststream::runtime::{Context, HandlerOutcome};
async fn handle<M: IncomingMessage>(_msg: &M, ctx: &mut Context<'_>) -> HandlerOutcome {
// The app's runtime metrics, read from a handler on a thread of its own.
let metrics = ctx.main_runtime().as_handle().metrics();
tracing::info!(
workers = metrics.num_workers(),
alive = metrics.num_alive_tasks(),
"app runtime"
);
HandlerOutcome::ack()
}Trait Implementations§
Source§impl Clone for MainRuntime
impl Clone for MainRuntime
Source§fn clone(&self) -> MainRuntime
fn clone(&self) -> MainRuntime
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl CtxKey for MainRuntime
impl CtxKey for MainRuntime
Source§type Value = MainRuntime
type Value = MainRuntime
Auto Trait Implementations§
impl Freeze for MainRuntime
impl RefUnwindSafe for MainRuntime
impl Send for MainRuntime
impl Sync for MainRuntime
impl Unpin for MainRuntime
impl UnsafeUnpin for MainRuntime
impl UnwindSafe for MainRuntime
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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