1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//! This crate provides a timeout queue based on [`fibers`] crate.
//!
//! [`fibers`]: https://github.com/dwango/fibers-rs
//!
//! # Examples
//!
//! ```
//! use fibers_timeout_queue::TimeoutQueue;
//! use std::time::Duration;
//! use std::thread;
//!
//! let mut queue = TimeoutQueue::new();
//! assert_eq!(queue.pop(), None); // `queue` is empty
//!
//! queue.push(1, Duration::from_millis(1000));
//! queue.push(2, Duration::from_millis(100));
//! queue.push(3, Duration::from_millis(10));
//! assert_eq!(queue.pop(), None); // No expired items
//!
//! thread::sleep(Duration::from_millis(50));
//! assert_eq!(queue.pop(), Some(3)); // There is an expired item
//! assert_eq!(queue.pop(), None);
//! ```
#![warn(missing_docs)]
extern crate fibers;
extern crate futures;

use fibers::time::timer::{self, Timeout};
use futures::{Async, Future};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::time::{Duration, SystemTime};

/// Timeout queue.
///
/// This contains items that to be dequeued when the associated timeouts have expired.
#[derive(Debug)]
pub struct TimeoutQueue<T> {
    queue: BinaryHeap<Item<T>>,
    next_timeout: Option<Timeout>,
}
impl<T> TimeoutQueue<T> {
    /// Makes a new `TimeoutQueue` instance.
    pub fn new() -> Self {
        TimeoutQueue {
            queue: BinaryHeap::new(),
            next_timeout: None,
        }
    }

    /// Enqueues the given item to the queue.
    ///
    /// The item will be dequeued by calling `pop` method after the specified timeout has expired.
    ///
    /// # Examples
    ///
    /// ```
    /// use fibers_timeout_queue::TimeoutQueue;
    /// use std::time::Duration;
    /// use std::thread;
    ///
    /// let mut queue = TimeoutQueue::new();
    /// queue.push(3, Duration::from_millis(5));
    ///
    /// assert_eq!(queue.pop(), None);
    /// thread::sleep(Duration::from_millis(10));
    /// assert_eq!(queue.pop(), Some(3));
    /// ```
    pub fn push(&mut self, item: T, timeout: Duration) {
        let expiry_time = SystemTime::now() + timeout;
        let reset_next_timeout = self
            .queue
            .peek()
            .map_or(false, |x| expiry_time < x.expiry_time);

        self.queue.push(Item { expiry_time, item });
        if reset_next_timeout {
            self.next_timeout = None;
        }
        self.poll_timeout();
    }

    /// Tries dequeuing an item of which timeout has expired.
    ///
    /// If there are no such items, this will return `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use fibers_timeout_queue::TimeoutQueue;
    /// use std::time::Duration;
    /// use std::thread;
    ///
    /// let mut queue = TimeoutQueue::new();
    /// assert_eq!(queue.pop(), None); // `queue` is empty
    ///
    /// queue.push(3, Duration::from_millis(5));
    /// assert_eq!(queue.pop(), None); // No expired items
    ///
    /// thread::sleep(Duration::from_millis(10));
    /// assert_eq!(queue.pop(), Some(3)); // There is an expired item
    /// ```
    pub fn pop(&mut self) -> Option<T> {
        self.filter_pop(|_| true)
    }

    /// A variant of `pop` method that filters items located in the queue's prefix.
    ///
    /// If the invocation of `filter(item)` returns `false`, the item will be discarded.
    ///
    /// This method is very efficient when there are many items that
    /// become unconscious before the timeout expires.
    ///
    /// # Examples
    ///
    /// ```
    /// use fibers_timeout_queue::TimeoutQueue;
    /// use std::time::Duration;
    /// use std::thread;
    ///
    /// let mut queue = TimeoutQueue::new();
    /// for i in 0..10 {
    ///     queue.push(i, Duration::from_millis(i));
    /// }
    /// assert_eq!(queue.filter_pop(|&n| n > 5), None);
    ///
    /// thread::sleep(Duration::from_millis(10));
    /// assert_eq!(queue.pop(), Some(6));
    /// ```
    pub fn filter_pop<F>(&mut self, filter: F) -> Option<T>
    where
        F: Fn(&T) -> bool,
    {
        let now = SystemTime::now();
        while let Some(x) = self.queue.pop() {
            if !filter(&x.item) {
                continue;
            }
            if x.expiry_time > now {
                self.queue.push(x);
                break;
            }
            return Some(x.item);
        }
        self.poll_timeout();
        None
    }

    /// Returns the number of items holded in the queue.
    ///
    /// # Examples
    ///
    /// ```
    /// use fibers_timeout_queue::TimeoutQueue;
    /// use std::time::Duration;
    ///
    /// let mut queue = TimeoutQueue::new();
    /// queue.push(1, Duration::from_millis(100));
    /// queue.push(2, Duration::from_millis(20));
    /// assert_eq!(queue.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.queue.len()
    }

    /// Returns `true` if the queue has no items, otherwise `false`.
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    fn poll_timeout(&mut self) {
        if let Ok(Async::Ready(_)) = self.next_timeout.poll() {
            self.next_timeout = self
                .queue
                .peek()
                .and_then(|x| x.expiry_time.duration_since(SystemTime::now()).ok())
                .map(timer::timeout);
        }
    }
}
impl<T> Default for TimeoutQueue<T> {
    fn default() -> Self {
        TimeoutQueue {
            queue: BinaryHeap::new(),
            next_timeout: None,
        }
    }
}

#[derive(Debug)]
struct Item<T> {
    expiry_time: SystemTime,
    item: T,
}
impl<T> PartialOrd for Item<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.expiry_time.partial_cmp(&self.expiry_time)
    }
}
impl<T> Ord for Item<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.expiry_time.cmp(&self.expiry_time)
    }
}
impl<T> PartialEq for Item<T> {
    fn eq(&self, other: &Self) -> bool {
        self.expiry_time == other.expiry_time
    }
}
impl<T> Eq for Item<T> {}

#[cfg(test)]
mod tests {
    use std::thread;

    use super::*;

    #[test]
    fn push_and_pop_works() {
        let mut queue = TimeoutQueue::new();
        assert!(queue.is_empty());

        queue.push(1, Duration::from_millis(20));
        queue.push(2, Duration::from_millis(10));
        assert_eq!(queue.pop(), None);
        assert_eq!(queue.len(), 2);

        thread::sleep(Duration::from_millis(12));
        assert_eq!(queue.pop(), Some(2));
        assert_eq!(queue.len(), 1);

        thread::sleep(Duration::from_millis(10));
        assert_eq!(queue.pop(), Some(1));
        assert_eq!(queue.len(), 0);
    }

    #[test]
    fn filter_pop_works() {
        let mut queue = TimeoutQueue::new();
        for i in 0..100 {
            queue.push(i, Duration::from_millis(i));
        }
        thread::sleep(Duration::from_millis(50));

        assert_eq!(queue.len(), 100);
        assert_eq!(queue.filter_pop(|n| *n == 25), Some(25));
        assert_eq!(queue.filter_pop(|n| *n == 80), None);
        assert_eq!(queue.len(), 20);

        thread::sleep(Duration::from_millis(50));
        assert_eq!(queue.pop(), Some(80));
        assert_eq!(queue.len(), 19);
    }
}