dejankos / blocking-delay-queue

Concurrent blocking delay queue

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Blocking delay queue

A thread safe blocking delay queue (bounded/unbounded) in which an element can only be taken when its delay has expired.
Supports adding and removing expired items by blocking until operation can be performed (add/take) or by waiting util timeout (offer/poll).

Example

use std::time::{Duration, Instant};

use blocking_delay_queue::{BlockingDelayQueue, DelayItem};

fn main() {
    // bounded queue
    let queue = BlockingDelayQueue::new_with_capacity(16);
    // there is also an unbounded impl -> BlockingDelayQueue::new_unbounded()

    // add element - blocks until item can be added respecting queue capacity
    queue.add(DelayItem::new(123, Instant::now()));
    // offer element - blocks until item can be added respecting queue capacity or the specified wait time expires
    let success = queue.offer(DelayItem::new(456, Instant::now()), Duration::from_secs(1));

    // take element - removes the head of this queue, waiting until an element is available
    let take = queue.take();
    // poll element - removes the head of this queue, waiting until an element is available or the specified wait time expires
    let poll = queue.poll(Duration::from_secs(1));

    // Removes all data from queue
    queue.clear();
    
    println!("Offering element status {}", success);
    println!("First element data {}", take.data);
    println!("Second element data {}", poll.unwrap().data);
    println!("Queue size {}", queue.size());
}

Benchmark

Run benchmark:

cargo bench

Criterion will create html reports under target dir.

Build from source

Install Rust

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Update to latest stable version

$ rustup update

Build

$ cargo build

License

Blocking delay queue is licensed under the MIT License

About

Concurrent blocking delay queue

License:MIT License


Languages

Language:Rust 100.0%