pub struct StreamMultiplexer { /* private fields */ }Expand description
Multiplexes multiple concurrent inference requests over a shared pipeline transport pair.
The PipelineStageRuntime send/recv methods process one message at a time.
When multiple requests are in flight (e.g. batched API requests), their
responses must be routed back to the correct caller by nonce.
StreamMultiplexer provides two complementary APIs:
- Request-response —
send_and_awaitsends anActivationMessageand suspends the caller until the matching reply (same nonce) arrives. - Async dispatch —
register_handlerregisters a one-shotoneshot::Senderthat is fired when a response with the matching nonce is delivered bydispatch_incoming.
§Typical usage
// Spawn one background task that continuously calls dispatch_incoming:
tokio::spawn(async move {
loop {
mux.dispatch_incoming(&mut stage).await.unwrap();
}
});
// Each request task calls send_and_await:
let response = mux.send_and_await(msg, &mut stage).await?;Implementations§
Source§impl StreamMultiplexer
impl StreamMultiplexer
Sourcepub fn register_handler(&mut self, nonce: u64) -> Receiver<ActivationMessage>
pub fn register_handler(&mut self, nonce: u64) -> Receiver<ActivationMessage>
Register a one-shot handler that fires when a response with nonce arrives.
Returns the corresponding oneshot::Receiver which the caller can
await to get the response. If a handler for the same nonce is already
registered, it is replaced and the old receiver will never fire.
Sourcepub async fn send_and_await(
&mut self,
msg: ActivationMessage,
stage: &mut PipelineStageRuntime,
) -> DistributedResult<ActivationMessage>
pub async fn send_and_await( &mut self, msg: ActivationMessage, stage: &mut PipelineStageRuntime, ) -> DistributedResult<ActivationMessage>
Send msg via stage and await the matching response.
This is a higher-level convenience that calls register_handler,
forwards the message to the next shard, and then awaits the registered
one-shot receiver.
§Errors
Returns DistributedError::Cancelled if the dispatch task drops the
sender before delivering a response (e.g. on transport error).
Sourcepub async fn dispatch_incoming(
&mut self,
stage: &mut PipelineStageRuntime,
) -> DistributedResult<()>
pub async fn dispatch_incoming( &mut self, stage: &mut PipelineStageRuntime, ) -> DistributedResult<()>
Receive one incoming message from stage and route it to the waiting
caller identified by its nonce.
This should be called repeatedly from a dedicated background task:
loop {
mux.dispatch_incoming(&mut stage).await?;
}Messages whose nonce is not in pending (e.g. unsolicited or already
cancelled) are silently dropped.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Number of requests currently awaiting a response.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StreamMultiplexer
impl !RefUnwindSafe for StreamMultiplexer
impl Send for StreamMultiplexer
impl Sync for StreamMultiplexer
impl Unpin for StreamMultiplexer
impl UnsafeUnpin for StreamMultiplexer
impl !UnwindSafe for StreamMultiplexer
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> 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