Fibre

A full suite of high performance, memory efficient, multimode sync/async, cancel-safe and strictly ordered channels.

fibre is a full suite of high performance, memory efficient, multimode sync/async, cancel-safe and strictly ordered channels for Rust.

Instead of one general-purpose queue it ships a separate channel for each producer and consumer pattern: spsc for one-to-one, mpsc for many-to-one, spmc for broadcast, spmc::topic for publish-subscribe, mpmc for many-to-many and oneshot for a single reply. Each one is built for its pattern, so a one-to-one pipe doesn't pay for the locking a many-to-many queue needs.

Depending on the type, a channel can be bounded, unbounded or a zero-capacity rendezvous. The handles share one set of method names with the same meaning on every type: send, try_send, recv, try_recv, recv_timeout and close. The four core channels also have a batch API that moves many items in one call. Async receivers implement futures::Stream, so you can drive them with StreamExt combinators.

Sync and async code can share a channel. Every sender and receiver on spsc, mpsc, spmc and mpmc has a zero-cost to_sync() or to_async(), so a producer on a plain OS thread can feed a consumer running in a Tokio task.

Shutdown works the same way everywhere. Drop every sender and the receivers drain what is already buffered before they see Disconnected, so closing a pipeline in order doesn't lose work.

use futures_util::StreamExt;

let (tx, mut rx) = fibre::mpsc::bounded_async::<u32>(16);
tokio::spawn(async move {
    for n in 0..3 {
        tx.send(n).await.unwrap();
    }
});
while let Some(n) = rx.next().await {
    println!("got {n}");
}

Benchmarks

Tutorials

Build an async job pipeline with fibreYou'll build a small Tokio program with three producers, a pool of workers and one collector. The producers push jobs into a bounded mpmc queue, the workers process them and an mpsc channel carries the results to the collector. The program shuts down on its own once the producers finish.Feed a Tokio task from a parser threadYou'll build a small program where an ordinary OS thread parses temperature readings and hands them to an async Tokio task over one spsc channel. The thread uses the channel's blocking API and the task uses its async API. Along the way you'll see what a full buffer does to the producer, how the channel closes from either end and how to move readings in batches.Build a request metrics aggregator with fibre mpscYou'll build a small Tokio program where several request handlers report every request they serve to one aggregator task, which owns the per-route counters and prints a report at the end. Along the way you'll swap between fibre's unbounded, bounded and rendezvous mpsc channels, see what a bounded send does when the channel is full and feed the same aggregator from a plain OS thread.Broadcast price ticks to every consumer with fibre spmcYou'll build a small Tokio program where one price feed sends ticks to several consumers over a bounded spmc channel and every consumer sees every tick. Along the way you'll add a consumer while the feed is running, watch the slowest consumer set the pace for the feed and let consumers leave early without breaking the others. The program shuts down on its own when the feed ends or when nobody is listening any more.Build a topic event bus with fibreYou'll build a small in-process event bus for a shop. One publisher sends events under the topic keys orders, payments and shipping. A handful of subscribers each receive only the topics they asked for. Along the way you'll change subscriptions while the bus is running, watch what happens to a subscriber that can't keep up and add a plain thread as a subscriber next to the async ones.Hand CPU work from async tasks to a thread poolYou'll build a small prime-counting service where async request handlers hand their work to a pool of plain OS threads over one mpmc channel. The handlers use the async end of the channel, the worker threads use the sync end and a separate sync thread submits a backlog in one batch. Along the way the workers free their memory when idle with recv_timeout and you'll swap the bounded queue for an unbounded one to see what changes.Build a request and reply actor with fibre oneshotYou'll build a small bank actor: one Tokio task owns the account balances and every other part of the program talks to it by sending a request with a oneshot reply channel inside. Along the way you'll see what a caller gets when the actor never answers, how to put a timeout on a reply, how a plain thread waits for one and how to reuse reply channels from a pool when requests come in fast.Build a pooled order pricer with fibre OneshotHostPoolYou'll build an order pricer where every order lives in a record taken from a fixed pool, with its reply channel embedded in the same record. A caller fills the record, hands the reply sender to a pricing task and reads the order back from the record when the price arrives. Along the way you'll see which side can read the record, what happens when every record is in use and how to take several records at once.

Also in Fibre