Skip to main content

Crate nexus_channel

Crate nexus_channel 

Source
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:

  1. Fast path: Try the operation immediately
  2. Backoff: Spin with exponential backoff using crossbeam::Backoff
  3. 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:

Metricnexus-channelcrossbeam-channelImprovement
p50 latency665 cycles1344 cycles2.0x
p999 latency2501 cycles37023 cycles14.8x
Throughput64 M msgs/sec34 M msgs/sec1.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.
RecvError
Error returned when receiving fails due to disconnection.
SendError
Error returned when sending fails due to disconnection.
Sender
The sending half of an SPSC channel.

Enums§

RecvTimeoutError
Error returned by recv_timeout.
TryRecvError
Error returned by try_recv.
TrySendError
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.