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,
impl<U, Err> Dispatcher<U, Err>where
U: Decoder + Encoder + 'static,
Err: 'static,
Sourcepub fn new<Io>(
io: Io,
codec: U,
service: Pipeline<DispatchItem<U>, Option<<U as Encoder>::Item>, Err>,
) -> Dispatcher<U, Err> ⓘwhere
IoBoxed: From<Io>,
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.
Sourcepub fn max_inflight(self, max: u32) -> Self
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.
Sourcepub fn keepalive_timeout(self, timeout: Seconds) -> Self
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.
Sourcepub fn skip_first_frame_timeout(self) -> Self
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.