pub struct HandlerCtx { /* private fields */ }Expand description
A context for handler requests that allow the handler to send notifications and spawn long-running tasks (e.g. subscriptions).
The handler is used for two things:
- Spawning long-running tasks (e.g. subscriptions) via
HandlerCtx::spawnorHandlerCtx::spawn_blocking. - Sending notifications to pubsub clients via
HandlerCtx::notify. Notifcations SHOULD be valid JSON-RPC objects, but this is not enforced by the type system.
Implementations§
Source§impl HandlerCtx
impl HandlerCtx
Sourcepub fn child_ctx<S: Clone + Send + Sync + 'static>(
&self,
router: &Router<S>,
parent: Option<&Span>,
) -> Self
pub fn child_ctx<S: Clone + Send + Sync + 'static>( &self, router: &Router<S>, parent: Option<&Span>, ) -> Self
Create a child handler context for a new handler invocation.
This is used when handling batch requests, to give each handler its own context.
Sourcepub const fn tracing_info(&self) -> &TracingInfo
pub const fn tracing_info(&self) -> &TracingInfo
Get a reference to the tracing information for this handler context.
Sourcepub const fn service_name(&self) -> &'static str
pub const fn service_name(&self) -> &'static str
Get the OpenTelemetry service name for this handler context.
Sourcepub fn set_tracing_info(&mut self, tracing: TracingInfo)
pub fn set_tracing_info(&mut self, tracing: TracingInfo)
Set the tracing information for this handler context.
Sourcepub fn notifications_enabled(&self) -> bool
pub fn notifications_enabled(&self) -> bool
Check if notifications can be sent to the client. This will be false when either the transport does not support notifications, or the notification channel has been closed (due the the client going away).
Sourcepub fn init_request_span<S>(
&self,
router: &Router<S>,
parent: Option<&Span>,
) -> &Span
pub fn init_request_span<S>( &self, router: &Router<S>, parent: Option<&Span>, ) -> &Span
Create a request span for a handler invocation.
Sourcepub async fn notify<T: RpcSend>(&self, t: &T) -> Result<(), NotifyError>
pub async fn notify<T: RpcSend>(&self, t: &T) -> Result<(), NotifyError>
Notify a client of an event.
Sourcepub fn spawn<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
pub fn spawn<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
Spawn a task on the task set. This task will be cancelled if the client disconnects. This is useful for long-running server tasks.
The resulting JoinHandle will contain None if the task was
cancelled, and Some otherwise.
Sourcepub fn spawn_with_ctx<F, Fut>(&self, f: F) -> JoinHandle<Option<Fut::Output>>
pub fn spawn_with_ctx<F, Fut>(&self, f: F) -> JoinHandle<Option<Fut::Output>>
Spawn a task on the task set with access to this context. This task will be cancelled if the client disconnects. This is useful for long-running tasks like subscriptions.
The resulting JoinHandle will contain None if the task was
cancelled, and Some otherwise.
Sourcepub fn spawn_blocking<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
pub fn spawn_blocking<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
Spawn a task that may block on the task set. This task may block, and will be cancelled if the client disconnects. This is useful for running expensive tasks that require blocking IO (e.g. database queries).
The resulting JoinHandle will contain None if the task was
cancelled, and Some otherwise.
Sourcepub fn spawn_blocking_with_ctx<F, Fut>(
&self,
f: F,
) -> JoinHandle<Option<Fut::Output>>
pub fn spawn_blocking_with_ctx<F, Fut>( &self, f: F, ) -> JoinHandle<Option<Fut::Output>>
Spawn a task that may block on the task set, with access to this context. This task may block, and will be cancelled if the client disconnects. This is useful for running expensive tasks that require blocking IO (e.g. database queries).
The resulting JoinHandle will contain None if the task was
cancelled, and Some otherwise.
Sourcepub fn spawn_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
pub fn spawn_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
Spawn a task on this task set. Unlike Self::spawn, this task will
NOT be cancelled if the client disconnects. Instead, it
is given a future that resolves when client disconnects. This is useful
for tasks that need to clean up resources before completing.
Sourcepub fn spawn_graceful_with_ctx<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>where
F: FnOnce(HandlerCtx, WaitForCancellationFutureOwned) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
pub fn spawn_graceful_with_ctx<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>where
F: FnOnce(HandlerCtx, WaitForCancellationFutureOwned) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
Spawn a task on this task set with access to this context. Unlike
Self::spawn, this task will NOT be cancelled if the client
disconnects. Instead, it is given a future that resolves when client
disconnects. This is useful for tasks that need to clean up resources
before completing.
Sourcepub fn spawn_blocking_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
pub fn spawn_blocking_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
Spawn a blocking task on this task set. Unlike Self::spawn_blocking,
this task will NOT be cancelled if the client disconnects. Instead, it
is given a future that resolves when client disconnects. This is useful
for tasks that need to clean up resources before completing.
Sourcepub fn spawn_blocking_graceful_with_ctx<F, Fut>(
&self,
f: F,
) -> JoinHandle<Fut::Output>where
F: FnOnce(HandlerCtx, WaitForCancellationFutureOwned) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
pub fn spawn_blocking_graceful_with_ctx<F, Fut>(
&self,
f: F,
) -> JoinHandle<Fut::Output>where
F: FnOnce(HandlerCtx, WaitForCancellationFutureOwned) -> Fut + Send + 'static,
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
Spawn a blocking task on this task set with access to this context.
Unlike Self::spawn_blocking, this task will NOT be cancelled if the
client disconnects. Instead, it is given a future that resolves when
the client disconnects. This is useful for tasks that need to clean up
resources before completing.
Trait Implementations§
Source§impl Clone for HandlerCtx
impl Clone for HandlerCtx
Source§fn clone(&self) -> HandlerCtx
fn clone(&self) -> HandlerCtx
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more