Skip to main content

Dispatcher

Struct Dispatcher 

Source
pub struct Dispatcher<U, Err>
where U: Encoder + Decoder + 'static, Err: 'static,
{ /* private fields */ }
Expand description

Future that dispatches decoded transport frames to a service.

Owns the transport, codec, and service pipeline supplied to Self::new. The pipeline accepts DispatchItem<U> and returns Option<<U as Encoder>::Item>. Successful responses are encoded into the transport’s write buffer; None produces no response.

Poll or await this local, non-Send future on the runtime owning the transport. Its output is Result<(), Err>; construction alone does not dispatch service events.

§Readiness and concurrency

Normal frame dispatch waits for pipeline readiness and space below the Self::max_inflight threshold. Service calls may overlap, and responses are encoded as calls complete rather than being reordered into request order. Use max_inflight(1) to serialize normal frame handling.

Write backpressure pauses frame decoding and produces Control::WBackPressureEnabled. When enough output drains, the dispatcher produces Control::WBackPressureDisabled and resumes normal processing. Pending calls continue during backpressure and may add more output. Control and stop events use the same readiness-aware pipeline as frames, but are not constrained by the normal frame-admission limit.

Pauses for service readiness or in-flight capacity suspend keep-alive and frame-read timers and reset the frame-read budget. A write-backpressure timeout continues running during these pauses. A paused transport filter suspends read-side and write timeouts instead, so filter delays are not charged to the peer.

§Stopping and shutdown

On a stop condition, the dispatcher attempts a DispatchItem::Stop call. That call can overlap earlier requests. Normally, after the stop call completes, the dispatcher drains all remaining service calls, waits for service shutdown, and then finishes transport shutdown. This drain also applies to transport failures; it has no general service-call deadline.

If pending stop delivery is blocked by readiness beyond the transport’s shutdown_timeout, stop delivery, draining, and service shutdown continue in a background task while the dispatcher finishes transport shutdown. At this handoff, the response future held by the dispatcher is cancelled; already-detached calls continue to drain. The dispatcher may therefore complete before service shutdown, and errors reported after handoff are not returned by it.

Only service errors are returned as Err; other stop reasons are reported as events, so Ok(()) may follow a codec, I/O, or timeout failure. Keep an IoRef and inspect IoRef::close_reason after completion to classify the termination; see the crate-level docs. A service error recorded before stop delivery takes precedence over a later stop-handler error. If none was recorded, a stop-handler error is returned when that call completes before handoff.

Dropping the dispatcher cancels the call futures it owns, but does not perform the normal stop-and-shutdown sequence or cancel already-detached calls. Drive the future to completion when orderly shutdown is required.

Implementations§

Source§

impl<U, Err> Dispatcher<U, Err>
where U: Decoder + Encoder + 'static, Err: 'static,

Source

pub fn new<Io>( io: Io, codec: U, service: Pipeline<DispatchItem<U>, Option<<U as Encoder>::Item>, Err>, ) -> Dispatcher<U, Err> ⓘ
where IoBoxed: From<Io>,

Creates a dispatcher for an I/O transport, codec, and service pipeline.

Accepts any transport convertible to [IoBoxed], including an [ntex_io::Io]. The codec must decode incoming frames and encode service responses, which may have different item types.

The pipeline must accept DispatchItem<U> and return Result<Option<<U as Encoder>::Item>, Err>. It already owns its service state and binding; this constructor does not bind the service again or invoke it.

Timeout and buffer policies come from the transport’s [ntex_io::IoConfig]. Construction clears any previous dispatcher timer and starts first-frame read-rate monitoring immediately when configured, before this future is first polled. Construct it on the runtime owning the transport, then poll or await it to drive dispatch.

Self::keepalive_timeout overrides the keep-alive timeout and Self::skip_first_frame_timeout cancels first-frame monitoring for this dispatcher only.

The default in-flight threshold is u32::MAX. Use Self::max_inflight to restrict normal frame admission.

Source

pub fn max_inflight(self, max: u32) -> Self

Limits normal frame admission by the number of in-flight service calls.

While at least max calls are pending, the dispatcher pauses normal frame dispatch. It resumes when the count falls below max and the service is ready. Set this to 1 to serialize normal frame handling.

Pending control and stop calls contribute to the count, but their delivery can exceed this threshold so that shutdown and flow-control processing can proceed. This is not a strict cap on all event calls or on the transport’s input and output buffers.

The default is u32::MAX, imposing no practical dispatcher-level concurrency limit.

§Panics

Panics if max is zero.

Source

pub fn keepalive_timeout(self, timeout: Seconds) -> Self

Overrides the keep-alive timeout for this dispatcher.

The default is the transport’s keepalive_timeout. [Seconds::ZERO] disables keep-alive. The transport configuration returned by IoRef::cfg is not changed.

Call this before the dispatcher is first polled.

Source

pub fn skip_first_frame_timeout(self) -> Self

Cancels the first-frame read-rate timer.

With a configured frame_read_rate, a new dispatcher requires the peer to start sending its first frame in time. This option lets the peer stay silent after connection setup, for example on a server-push protocol. The keep-alive timeout still applies while the connection is idle, and the frame read rate applies once data arrives.

Has no effect when no frame read rate is configured. Call this before the dispatcher is first polled.

Trait Implementations§

Source§

impl<U: Encoder + Decoder, Err> Debug for Dispatcher<U, Err>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<U, Err> Future for Dispatcher<U, Err>
where U: Decoder + Encoder + 'static, Err: 'static,

Source§

type Output = Result<(), Err>

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more
Source§

impl<U: Encoder + Decoder, Err> Unpin for Dispatcher<U, Err>

Auto Trait Implementations§

§

impl<U, Err> !RefUnwindSafe for Dispatcher<U, Err>

§

impl<U, Err> !Send for Dispatcher<U, Err>

§

impl<U, Err> !Sync for Dispatcher<U, Err>

§

impl<U, Err> !UnwindSafe for Dispatcher<U, Err>

§

impl<U, Err> Freeze for Dispatcher<U, Err>
where Err: Freeze,

§

impl<U, Err> UnsafeUnpin for Dispatcher<U, Err>
where Err: UnsafeUnpin,

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
§

impl<F> OnDropFutureExt for F
where F: Future,

§

fn on_drop<F>(self, on_drop: F) -> OnDropFuture<Self, F> ⓘ
where F: FnOnce(),

Wraps this future so that on_drop is called if the future is dropped before it completes. The callback is cancelled once the future completes, whatever its output.
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

impl<F, T, E> TryFuture for F
where F: Future<Output = Result<T, E>> + ?Sized,

§

type Ok = T

The type of successful values yielded by this future
§

type Error = E

The type of failures yielded by this future
§

fn try_poll( self: Pin<&mut F>, cx: &mut Context<'_>, ) -> Poll<<F as Future>::Output>

Poll this TryFuture as if it were a Future. Read more
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V