#queue #object #minimum #set #maintain #time #messages

bin+lib timed-queue

Maintain a set of objects and the minimum time at which they should be returned

5 releases

0.2.3 Dec 29, 2020
0.2.2 Dec 29, 2020
0.2.1 Dec 29, 2020
0.2.0 Dec 28, 2020
0.1.0 Dec 28, 2020

#11 in #maintain

Download history 114/week @ 2024-07-27 194/week @ 2024-08-03 196/week @ 2024-08-10 136/week @ 2024-08-17 111/week @ 2024-08-24 142/week @ 2024-08-31 114/week @ 2024-09-07 30/week @ 2024-09-14 108/week @ 2024-09-21 33/week @ 2024-09-28 147/week @ 2024-10-05 65/week @ 2024-10-12 5/week @ 2024-10-19 20/week @ 2024-10-26 19/week @ 2024-11-02 12/week @ 2024-11-09

56 downloads per month

MIT license

6KB
120 lines

Documentation is at https://docs.rs/timed-queue .


lib.rs:

timed-queue provides TimedQueue, a set of objects and the minimum time at which they should be returned.

Example

Imagine the "new messages" queue of an SMTP server implementation. Delivery should be attempted immediately for new messages. Messages for which delivery fails should be retried after 30 minutes.

 fn server_loop<I: IntoIterator<Item = MailMessage>>(tq: TimedQueue<MailMessage>, messages: I) {
     for m in messages {
         tq.enqueue(m, None);
     }
 }

 async fn delivery_loop(tq: TimedQueue<MailMessage>) {
     loop {
         let (msg, _) = tq.dequeue().await;
         if try_deliver(msg).await.is_err() {
             tq.enqueue(msg, Some(Instant::now() + Duration::from_secs(30 * 60)));
         }
     }
 }

 #[tokio::main]
 async fn main() {
     let tq = TimedQueue::new();
     let tq2 = tq.clone();
     std::thread::spawn(move || server_loop(tq, get_message_stream()));
     tokio::spawn(delivery_loop(tq2));
 }

Dependencies

~2.4–8MB
~58K SLoC