7 releases (2 stable)

1.0.1 Aug 29, 2023
1.0.0 Dec 3, 2022
0.1.7 Aug 21, 2021
0.1.5 Jun 28, 2021
0.1.3 Jun 29, 2020

#160 in Concurrency

Download history 1229/week @ 2024-07-21 1228/week @ 2024-07-28 1003/week @ 2024-08-04 819/week @ 2024-08-11 978/week @ 2024-08-18 1472/week @ 2024-08-25 1006/week @ 2024-09-01 1003/week @ 2024-09-08 1287/week @ 2024-09-15 1028/week @ 2024-09-22 1087/week @ 2024-09-29 1139/week @ 2024-10-06 1047/week @ 2024-10-13 1360/week @ 2024-10-20 1617/week @ 2024-10-27 1500/week @ 2024-11-03

5,589 downloads per month

Apache-2.0

140KB
4K SLoC

Crossfire

Build Status License Cargo Documentation Rust 1.36+

This crate provide channels used between async-async or async-blocking code, in all direction. Implmented with lockless in mind, low level is based on crossbeam-channel

Performance

Faster than channel in std or mpsc in tokio, slightly slower than crossbeam itself (since async overhead to wake up sender or receiver).

Run the benchmark tests to see for yourself:

cargo test performance --release -- --nocapture --test-threads=1

APIs

Usage

Add this to your Cargo.toml:

[dependencies]
crossfire = "0.1"

extern crate crossfire;
extern crate tokio;

use crossfire::mpsc;

// async-async

let (tx, rx) = mpsc::bounded_future_both::<i32>(100);
tokio::spawn(async move {
    for i in 0i32..10000 {
        let _ = tx.send(i).await;
        println!("sent {}", i);
    }
});

loop {
    if let Ok(_i) = rx.recv().await {
        println!("recv {}", _i);
    } else {
        println!("rx closed");
        break;
    }
}

mpmc & mpsc package is almost the same, while mpsc has some optimization becauses it assumes only one consumer.

Error types are re-exported from crossbeam-channel.

Compatibility

Supports stable Rust. Mainly tested on tokio-0.2 (Not tested on async-std or other runtime). future::selects and timeout work fine, but it takes advantage of runtime behavior not documented by Rust official.

Refer to https://github.com/rust-lang/rust/issues/73002

Memory overhead

While using mp tx or mp rx, there's memory overhead to pass along wakers for pending async producer or consumer. Since waker is small, the overhead can be ignored if your channel is busy. Canceled wakers will be eventually cleanup by later send/receive event. If the channel is used for close notification (which never trigger) in combine with futures::select, currently there's hard coded threshold to clean up those canceled wakers.

Stability

This channel implementation serves in various components of our storage engine, you are welcome to rely on it.

Dependencies

~1–1.6MB
~33K SLoC