Skip to content

dejankos/blocking-delay-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages