Skip to main content

HandlerCtx

Struct HandlerCtx 

Source
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:

Implementations§

Source§

impl HandlerCtx

Source

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.

Source

pub const fn tracing_info(&self) -> &TracingInfo

Get a reference to the tracing information for this handler context.

Source

pub const fn service_name(&self) -> &'static str

Get the OpenTelemetry service name for this handler context.

Source

pub fn span(&self) -> &Span

Get a reference to the tracing span for this handler context.

Source

pub fn set_tracing_info(&mut self, tracing: TracingInfo)

Set the tracing information for this handler context.

Source

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).

Source

pub fn notification_capacity(&self) -> usize

Returns the maximum capacity of the notification channel, or 0 if notifications are not enabled.

Source

pub fn init_request_span<S>( &self, router: &Router<S>, parent: Option<&Span>, ) -> &Span
where S: Clone + Send + Sync + 'static,

Create a request span for a handler invocation.

Source

pub async fn notify<T: RpcSend>(&self, t: T) -> Result<(), NotifyError>

Notify a client of an event.

Takes t by value to support consuming RpcSend implementations. For Serialize types, pass a reference (&item) — references implement RpcSend via the blanket impl.

Source

pub async fn notify_stream<S, T>(&self, stream: S) -> Result<(), NotifyError>
where S: Stream<Item = T> + Send, T: RpcSend,

Forward a Stream into the notification channel.

Each item produced by the stream is serialized and sent as a notification. This continues until the stream is exhausted or an error occurs.

If notifications are not enabled on this context, the stream is not consumed and this returns Ok(()) immediately.

Source

pub async fn permit(&self) -> Option<NotifyPermit<'_>>

Reserve a permit to send a single notification.

Returns None if notifications are not enabled or the channel is closed. Awaits until a channel slot is available.

Source

pub async fn permit_owned(&self) -> Option<OwnedNotifyPermit>

Reserve an owned permit to send a single notification.

Returns None if notifications are not enabled or the channel is closed. Awaits until a channel slot is available.

Source

pub fn try_permit(&self) -> Option<NotifyPermit<'_>>

Try to reserve a permit to send a single notification.

Returns None if notifications are not enabled, the channel is closed, or the channel is full.

Source

pub fn try_permit_owned(&self) -> Option<OwnedNotifyPermit>

Try to reserve an owned permit to send a single notification.

Returns None if notifications are not enabled, the channel is closed, or the channel is full.

Source

pub async fn permit_many( &self, n: usize, ) -> Option<impl Iterator<Item = OwnedNotifyPermit>>

Reserve n owned permits to send notifications.

This is all-or-nothing: returns None if the channel closes during acquisition, dropping any already-acquired permits. Returns None if notifications are not enabled.

Source

pub fn try_permit_many( &self, n: usize, ) -> Option<impl Iterator<Item = OwnedNotifyPermit>>

Try to reserve n owned permits to send notifications.

This is all-or-nothing: returns None if any permit cannot be acquired. Returns None if notifications are not enabled, the channel is closed, or the channel has fewer than n available slots.

Source

pub fn spawn<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
where F: Future + Send + 'static, F::Output: Send + 'static,

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.

Source

pub fn spawn_with_ctx<F, Fut>(&self, f: F) -> JoinHandle<Option<Fut::Output>>
where F: FnOnce(HandlerCtx) -> Fut, Fut: Future + Send + 'static, Fut::Output: Send + 'static,

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.

Source

pub fn spawn_notify_stream<S, T>( &self, stream: S, ) -> JoinHandle<Option<Result<(), NotifyError>>>
where S: Stream<Item = T> + Send + 'static, T: RpcSend + 'static,

Spawn a task that forwards a Stream into the notification channel. The task will be cancelled if the client disconnects.

The resulting JoinHandle will contain None if the task was cancelled, and Some otherwise.

Source

pub fn spawn_blocking<F>(&self, f: F) -> JoinHandle<Option<F::Output>>
where F: Future + Send + 'static, F::Output: Send + 'static,

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.

Source

pub fn spawn_blocking_with_ctx<F, Fut>( &self, f: F, ) -> JoinHandle<Option<Fut::Output>>
where F: FnOnce(HandlerCtx) -> Fut, Fut: Future + Send + 'static, Fut::Output: Send + 'static,

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.

Source

pub fn spawn_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
where F: FnOnce(WaitForCancellationFutureOwned) -> Fut + Send + 'static, Fut: Future + Send + 'static, Fut::Output: Send + 'static,

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.

Source

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.

Source

pub fn spawn_blocking_graceful<F, Fut>(&self, f: F) -> JoinHandle<Fut::Output>
where F: FnOnce(WaitForCancellationFutureOwned) -> Fut + Send + 'static, Fut: Future + Send + 'static, Fut::Output: Send + 'static,

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.

Source

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

Source§

fn clone(&self) -> HandlerCtx

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HandlerCtx

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryClone for T
where T: Clone,

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,