#queue #object #set #minimum #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

#12 in #maintain

Download history 10/week @ 2024-03-28 8/week @ 2024-04-04 43/week @ 2024-05-02 44/week @ 2024-05-09 143/week @ 2024-05-16 126/week @ 2024-05-23 217/week @ 2024-05-30 118/week @ 2024-06-06 180/week @ 2024-06-13 140/week @ 2024-06-20 86/week @ 2024-06-27

555 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–8.5MB
~57K SLoC