Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Two Channels, Two Directions

Submit

    Owns or controls
    Why this boundary exists
    Open the runnable source ↗

    The work channel

    Tokio’s bounded mpsc channel supports multiple cloned senders and one dispatcher receiver. It packages FIFO storage, wakeups, closure, and async capacity waiting. A VecDeque supplies only storage; recreating the channel would also require a mutex, notifications, capacity accounting, and a closed state.

    The result channel

    Submission creates oneshot::channel::<Result<T, E>>(). The wrapper moved into the work queue owns Sender<Result<T, E>>; JobHandle<T, E> owns the receiver. send is synchronous: it moves the result into the channel’s single shared slot and optionally wakes the receiver.

    If the caller waits ten seconds before awaiting, the result simply remains in that slot. The worker has already finished and released its concurrency slot. If the caller drops the handle, send returns the result as an error and the wrapper drops it. No worker waits for consumption.

    This is why a global response queue would be worse: it would require IDs, type erasure for results, demultiplexing, and fairness between unrelated callers.