Expand description
High-performance bounded SPSC channel for low-latency systems.
This crate provides a blocking single-producer single-consumer channel optimized for trading systems and other latency-critical workloads.
For MPSC (multi-producer), use crossbeam-channel
which is well-optimized for that use case. For raw MPSC queue performance without
blocking semantics, see nexus-queue::mpsc.
§Design
The channel uses a three-phase backoff strategy that minimizes syscall overhead:
- Fast path: Try the operation immediately
- Backoff: Spin with exponential backoff using
crossbeam::Backoff - Park: Sleep until woken by the other end
The key optimization is conditional parking: we only issue expensive unpark syscalls when the other end has actually gone to sleep. This dramatically reduces tail latency compared to channels that unpark unconditionally.
§Quick Start
use nexus_channel::channel;
let (tx, rx) = channel::<u64>(1024);
tx.send(42).unwrap();
assert_eq!(rx.recv().unwrap(), 42);§Timeout Support
use nexus_channel::{channel, RecvTimeoutError};
use std::time::Duration;
let (tx, rx) = channel::<u64>(4);
match rx.recv_timeout(Duration::from_millis(100)) {
Ok(value) => println!("got {}", value),
Err(RecvTimeoutError::Timeout) => println!("timed out"),
Err(RecvTimeoutError::Disconnected) => println!("sender dropped"),
}§Performance
Benchmarked against crossbeam-channel on Intel Core Ultra 7 @ 2.7GHz,
pinned to physical cores with turbo disabled:
| Metric | nexus-channel | crossbeam-channel | Improvement |
|---|---|---|---|
| p50 latency | 665 cycles | 1344 cycles | 2.0x |
| p999 latency | 2501 cycles | 37023 cycles | 14.8x |
| Throughput | 64 M msgs/sec | 34 M msgs/sec | 1.9x |
The large p999 improvement comes from avoiding unnecessary syscalls.
Modules§
- spsc
- Single-producer single-consumer bounded channel (re-export).
Structs§
- Receiver
- The receiving half of an SPSC channel.
- Recv
Error - Error returned when receiving fails due to disconnection.
- Send
Error - Error returned when sending fails due to disconnection.
- Sender
- The sending half of an SPSC channel.
Enums§
- Recv
Timeout Error - Error returned by
recv_timeout. - TryRecv
Error - Error returned by
try_recv. - TrySend
Error - Error returned by
try_send.
Functions§
- channel
- Creates a bounded SPSC channel with the given capacity.
- channel_
with_ config - Creates a bounded SPSC channel with custom backoff configuration.