Skip to content

Repository files navigation

Deadqueue Latest VersionBuild Status

Deadqueue is a dead simple async queue with back pressure support.

This crate provides three implementations:

  • Unlimited (deadqueue::unlimited::Queue)

    • Based on crossbeam_queue::SegQueue
    • Has unlimitied capacity and no back pressure on push
    • Enabled via the unlimited feature in your Cargo.toml
  • Resizable (deadqueue::resizable::Queue)

    • Based on deadqueue::unlimited::Queue
    • Has limited capacity with back pressure on push
    • Supports resizing
    • Enabled via the resizable feature in your Cargo.toml
  • Limited (deadqueue::limited::Queue)

    • Based on crossbeam_queue::ArrayQueue
    • Has limit capacity with back pressure on push
    • Does not support resizing
    • Enabled via the limited feature in your Cargo.toml

Features

FeatureDescriptionExtra dependenciesDefault
unlimitedEnable unlimited queue implementationyes
resizableEnable resizable queue implementationdeadqueue/unlimitedyes
limitedEnable limited queue implementationyes

Example

use std::sync::Arc;use tokio::time::{sleep,Duration};constTASK_COUNT:usize = 1000;constWORKER_COUNT:usize = 10;typeTaskQueue = deadqueue::limited::Queue<usize>;#[tokio::main]asyncfnmain(){let queue = Arc::new(TaskQueue::new(TASK_COUNT));for i in0..TASK_COUNT{
queue.try_push(i).unwrap();}for worker in0..WORKER_COUNT{let queue = queue.clone();
tokio::spawn(asyncmove{loop{let task = queue.pop().await;println!("worker[{}] processing task[{}] ...", worker, task);}});}println!("Waiting for workers to finish...");
queue.wait_empty().await;println!("All tasks done. :-)");}

Reasons for yet another queue

Deadqueue is by no means the only queue implementation available. It does things a little different and provides features that other implementations are lacking:

  • Resizable queue. Usually you have to pick between limited and unlimited queues. This crate features a resizable Queue which can be resized as needed. This is probably a big unique selling point of this crate.

  • Introspection support. The methods .len(), .capacity() and .available() provide access the current state of the queue.

  • Fair scheduling. Tasks calling pop will receive items in a first-come-first-serve fashion. This is mainly due to the use of tokio::sync::Semaphore which is fair by nature.

  • One struct, not two. The channels of tokio, async_std and futures-intrusive split the queue in two structs (Sender and Receiver) which makes the usage sligthly more complicated.

  • Bring your own Arc. Since there is no separation between Sender and Receiver there is also no need for an internal Arc. (All implementations that split the channel into a Sender and Receiver need some kind of Arc internally.)

  • Fully concurrent access. No need to wrap the Receiver part in a Mutex. All methods support concurrent accesswithout the need for an additional synchronization primitive.

  • Support for try__ methods. The methods try_push and try_pop can be used to access the queue from non-blocking synchroneous code.

  • Support for detecting when the queue becomes empty or full, using the wait_empty, subscribe_empty, wait_full and subscribe_full methods.

Alternatives

CrateLimitationsDocumentation
tokioNo resizable queue. No introspection support. Synchronization of Receiver needed.tokio::sync::mpsc::channel, tokio::sync::mpsc::unbounded_channel
async-stdNo resizable or unlimited queue. No introspection support. No try_send or try_recv methods.async_std::sync::channel
futuresNo resizable queue. No introspection support.futures::channel::mpsc::channel, futures::channel::mpsc::unbounded

License

Licensed under either of

at your option.

About

Dead simple queue implementation for rust with async-await

Resources

Stars

112 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages