Code Monkey home page Code Monkey logo

blocking-delay-queue's Introduction

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

blocking-delay-queue's People

Contributors

dejankos avatar marcbull avatar

Watchers

 avatar

Forkers

marcbull

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.