A collection of high performance concurrent channels.

Related tags

Concurrency firefly
Overview

firefly

A collection of high performance concurrent channels.

// create an unbounded channel
let (tx, rx) = firefly::mpsc::unbounded();

thread::spawn(move || {
    // send a message across
    tx.send(42).unwrap();
});

// receive the message
assert_eq!(rx.recv_blocking().unwrap(), 42);

Channel Flavors

Firefly provides a variety of channel flavors, optimized for specific use cases:

  • [spsc::bounded]
  • [spsc::unbounded]
  • [mpsc::bounded]
  • [mpsc::unbounded]
  • [mpmc::bounded]
  • [mpmc::unbounded]

In general a channel flavor higher up on the list is likely to be more performant than a more generic one lower down.


Bounded channels are created with a bounded capacity; the maximum number of messages that can be held at a given time:

// create a channel that can hold at most 10 messages at a time
let (tx, rx) = firefly::mpsc::bounded(10);

thread::spawn(move || {
    for i in 0..100 {
        // send a message, potentially blocking until capacity frees up
        tx.send_blocking(i).unwrap();
    }
});

for _ in 0..100 {
    // wait for a message to be sent
    let i = rx.recv_blocking().unwrap();
    println!("{i}");
}

Unbounded channels on the other hand are unlimited in their capacity, meaning that sending never blocks:

// create an unbounded channel
let (tx, rx) = firefly::mpsc::unbounded();

thread::spawn(move || {
    // send an infinite amount of messages
    for i in 0.. {
        tx.send(i).unwrap();
    }
});

// receive an infinite amount of messages
loop {
    let i = rx.recv_blocking().unwrap();
    println!("{i}");
}

Blocking

Send and receive operations can be performed four different ways:

  • Non-blocking (returns immediately with success or failure).
  • Blocking (blocks the thread until the operation succeeds or the channel disconnects).
  • Blocking with a timeout (blocks upto a maximum duration of time).
  • Asynchronously (blocks the async task).
assert_eq!(x, 42), Err(_) => println!("message took too long to send") } tokio::spawn(async move { // block the async task until the message is sent assert_eq!(rx.recv().await.unwrap(), 42); });">
use std::time::Duration;

let (tx, rx) = firefly::mpsc::bounded(4);

thread::spawn(move || {
    for _ in 0..3 {
        // this can never fail because we only ever send
        // 3 messages, and the capacity is 4
        tx.try_send(42).unwrap();
    }
});

// receive the message or return an error if not immediately ready
match rx.try_recv() {
    Ok(x) => assert_eq!(x, 42),
    Err(_) => println!("message has not been sent yet")
}

// block the thread until the message is sent
assert_eq!(rx.recv_blocking().unwrap(), 42);

// block the thread upto 100ms
match rx.recv_timeout(Duration::from_millis(100)) {
    Ok(x) => assert_eq!(x, 42),
    Err(_) => println!("message took too long to send")
}

tokio::spawn(async move {
    // block the async task until the message is sent
    assert_eq!(rx.recv().await.unwrap(), 42);
});

Channels can also be used as "bridge" channels between async and sync code:

let (tx, rx) = firefly::mpsc::bounded(10);

thread::spawn(move || {
    // send messages synchronously
    for i in 0.. {
        tx.send_blocking(i).unwrap()
    }
});

tokio::spawn(async move {
    // receive asynchronously
    loop {
        let i = rx.recv().await.unwrap();
        println!("{i}");
    }
});

Disconnection

When all senders or receivers of a given channel are dropped, the channel is disconnected. Any attempts to send a message will fail. Any remaining messages in the channel can be received, but subsequent attempts to receive will also fail:

let (tx, rx) = firefly::mpsc::unbounded();
tx.send(1).unwrap();
tx.send(2).unwrap();

// drop the sender
drop(tx);

// any remaining messages can be received
assert_eq!(r.recv(), Ok(1));
assert_eq!(r.recv(), Ok(2));

// subsequent attempts will error
assert_eq!(r.recv(), Err(RecvError));
You might also like...
Remoc 🦑 — Remote multiplexed objects and channels for Rust
Remoc 🦑 — Remote multiplexed objects and channels for Rust

Remoc 🦑 — remote multiplexed objects and channels Remoc makes remote interaction between Rust programs seamless and smooth. Over a single underlying

ever wanted to leave all slack channels? now you can!

slack-leaver ever wanted to leave all slack channels? now you can! usage Head to the Releases page, pick a release that matches your platform. from so

An async-ready Phoenix Channels v2 client library in Rust

Phoenix Channels This crate implements a Phoenix Channels (v2) client in Rust. Status NOTE: This client is still a work-in-progress, though it has eno

Powerfull Discord Raid Bot written in Rust, use VPN / Proxy because creating 200 channels in 10s Will ratelimit you.

Harakiri-Rust This the first Discord Raid Bot made in RustLang I recommend you use with a VPN or a Proxy to evade Discord Ratelimit. If bot doesn't st

Platform independent data channels for WebRTC/Rust.

preach Platform independent data channels Preach provides an abstraction for WebRTC data channels that runs on both native and web platforms. Preach m

Small Rust program for sending messages to Telegram channels.

tg-send: a small Rust program for sending Telegram messages Send messages to a group/channel via the Bot API from the command line; it's super simple

The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language.

rcc The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language. Compilers Language Co

A collection of compilers based around compiling a high level language to a Brainfuck dialect.
A collection of compilers based around compiling a high level language to a Brainfuck dialect.

tf A collection of compilers based around compiling a high level language to a Brainfuck dialect. Built at, and for, the VolHacks V hackathon during O

Tools for concurrent programming in Rust

Crossbeam This crate provides a set of tools for concurrent programming: Atomics AtomicCell, a thread-safe mutable memory location.(no_std) AtomicCons

An Extensible, Concurrent Web Framework for Rust

Iron Extensible, Concurrency Focused Web Development in Rust. Response Timer Example Note: This example works with the current iron code in this repos

Shuttle is a library for testing concurrent Rust code

Shuttle Shuttle is a library for testing concurrent Rust code. It is an implementation of a number of randomized concurrency testing techniques, inclu

Spawn multiple concurrent unix terminals in Discord

Using this bot can be exceedingly dangerous since you're basically granting people direct access to your shell.

A Rust synchronisation primitive for "Multiplexed Concurrent Single-Threaded Read" access

exit-left verb; 1. To exit or disappear in a quiet, non-dramatic fashion, making way for more interesting events. 2. (imperative) Leave the scene, and

A Multitask Parallel Concurrent Executor for ns-3 (network simulator)

A Multitask Parallel Concurrent Executor for ns-3 (network simulator)

Real Time For the Masses (RTFM), a framework for building concurrent applications, for MSP430 MCUs

msp430-rtfm Real Time For the Masses (RTFM), a framework for building concurrent applications, for MSP430 MCUs License Licensed under either of Apache

Fast, efficient, and robust memory reclamation for concurrent data structures

Seize Fast, efficient, and robust memory reclamation for concurrent data structures. Introduction Concurrent data structures are faced with the proble

A concurrent, append-only vector
A concurrent, append-only vector

The vector provided by this crate suports concurrent get and push operations. Reads are always lock-free, as are writes except when resizing is required.

wait-free 4-level 64-bit pagetable for contiguous low-contention concurrent metadata

pagetable Wait-free 4-level page table that maps from a u64 key to an &AtomicU64 value. Page fan-out is 2^16. If a key doesn't exist, intermediate pag

Log for concurrent workloads, with support for atomic batches and in-order recovery

sharded-log A batch-oriented multi-threaded sharded log for workloads that occasionally flush logs into some other system. All batches have a 32-bit C

Owner
Ibraheem Ahmed
Freelance software developer interested in building fast, concurrent, and robust systems.
Ibraheem Ahmed
Cogo is a high-performance library for programming stackful coroutines with which you can easily develop and maintain massive concurrent programs.

Cogo is a high-performance library for programming stackful coroutines with which you can easily develop and maintain massive concurrent programs.

co-rs 47 Nov 17, 2022
High-performance, lock-free local and concurrent object memory pool with automated allocation, cleanup, and verification.

Opool: Fast lock-free concurrent and local object pool Opool is a high-performance Rust library that offers a concurrent and local object pool impleme

Khashayar Fereidani 8 Jun 3, 2023
Für Elise (short for Elise) is a concurrent garbage collection attempt based on shifgrethor.

Für Elise (short for Elise) is a concurrent garbage collection attempt based on shifgrethor. The goal is to define an API for precise, tracing garbage collection in Rust which upholds all of Rust's safety guarantees. A user using the API defined in this library will not be at risk for any of the kinds of memory errors that Rust can prevent.

Ngo Iok Ui (Wu Yu Wei) 38 Dec 29, 2022
syncmap is a fast, concurrent cache library built with a focus on performance and correctness.

syncmap syncmap syncmap is a fast, concurrent cache library syncmap is a fast, concurrent cache library built with a focus on performance and correctn

Behrouz R.Farsi 15 Dec 2, 2022
A high-performance, high-reliability observability data pipeline.

Quickstart • Docs • Guides • Integrations • Chat • Download What is Vector? Vector is a high-performance, end-to-end (agent & aggregator) observabilit

Timber 12.1k Jan 2, 2023
Program to check if stereo wav files have identical channels (faux-stereo) and convert them to mono.

zrtstr Command line application for checking WAV-files for identical channels, detecting faux-stereo files generated by some audio-editing software an

Kirill 22 Nov 6, 2022
A CLI application which allows you to archive Urbit channels and all linked content in them.

The Urbit Content Archiver is a small CLI application that exports channels from your Urbit ship and auto-downloads any directly linked content locall

Robert Kornacki 33 Sep 25, 2022
Locast to Emby/Plex/Channels server

This application provides an interface between locast.org and Media Servers like Plex Media Server (PMS) and Emby by acting like an HDHomerun or an m3u tuner and an XMLTV provider.

Wouter de Bie 51 Sep 10, 2022
A tool to subscribe to Twitch channels and store them efficiently on disk

twitch-messages A tool to subscribe to Twitch channels and store them efficiently on disk Build the Tools You can start by building the binaries that

Clément Renault 1 Oct 31, 2021
A tool to stream the chats of Twitch channels as a CSV.

twitch2csv A tool to stream the chats of Twitch channels as a CSV. Installation You can use cargo to install this tool: cargo install -f twitch2csv Us

Clément Renault 2 Nov 20, 2021