relay_server/utils/scheduled/
queue.rs

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
255
256
257
258
259
260
261
262
263
264
265
266
267
use priority_queue::PriorityQueue;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

use futures::stream::FusedStream;
use tokio::time::Instant;

use futures::Stream;

/// A scheduled queue that can be polled for when the next item is ready.
pub struct ScheduledQueue<T> {
    queue: BinaryHeap<Item<T>>,
    sleep: Pin<Box<tokio::time::Sleep>>,
}

impl<T> ScheduledQueue<T> {
    /// Schedules a new item to be yielded at `when`.
    pub fn schedule(&mut self, when: Instant, value: T) {
        self.queue.push(Item { when, value });
    }

    fn peek_when(&self) -> Option<Instant> {
        self.queue.peek().map(|item| item.when)
    }

    fn pop_value(&mut self) -> Option<T> {
        self.queue.pop().map(|item| item.value)
    }

    fn iter(&self) -> impl Iterator<Item = (Instant, &T)> + '_ {
        self.queue.iter().map(|item| (item.when, &item.value))
    }
}

/// A scheduled queue that can be polled for when the next item is ready.
///
/// Unlike [`ScheduledQueue`] every unique `T` can only be scheduled once,
/// scheduling a value again moves the deadline instead.
pub struct UniqueScheduledQueue<T>
where
    T: std::hash::Hash + Eq,
{
    queue: PriorityQueue<T, Reverse<Instant>>,
    sleep: Pin<Box<tokio::time::Sleep>>,
}

impl<T: std::hash::Hash + Eq> UniqueScheduledQueue<T> {
    /// Schedules an item to be yielded at `when`.
    ///
    /// If the item was net yet scheduled, it is inserted into the queue,
    /// otherwise the previous schedule is moved to the new deadline.
    pub fn schedule(&mut self, when: Instant, value: T) {
        self.queue.push(value, Reverse(when));
    }

    /// Removes a value from the queue.
    pub fn remove(&mut self, value: &T) {
        self.queue.remove(value);
    }

    fn peek_when(&self) -> Option<Instant> {
        self.queue.peek().map(|(_, Reverse(when))| *when)
    }

    fn pop_value(&mut self) -> Option<T> {
        self.queue.pop().map(|(value, _)| value)
    }

    fn iter(&self) -> impl Iterator<Item = (Instant, &T)> + '_ {
        self.queue
            .iter()
            .map(|(value, Reverse(when))| (*when, value))
    }
}

macro_rules! impl_queue {
    ($name:ident, $($where:tt)*) => {
        impl<T: $($where)*> $name<T> {
            /// Creates a new, empty [`Self`].
            pub fn new() -> Self {
                Self {
                    queue: Default::default(),
                    sleep: Box::pin(tokio::time::sleep(Duration::MAX)),
                }
            }

            /// Returns the current size of the queue.
            #[allow(dead_code)]
            pub fn len(&self) -> usize {
                self.queue.len()
            }

            /// Returns true if there are no items in the queue.
            #[allow(dead_code)]
            pub fn is_empty(&self) -> bool {
                self.len() == 0
            }
        }

        impl<T: $($where)*> Default for $name<T> {
            fn default() -> Self {
                Self::new()
            }
        }

        impl<T: $($where)*> Unpin for $name<T> {}

        impl<T: $($where)*> FusedStream for $name<T> {
            fn is_terminated(&self) -> bool {
                // The stream never returns `Poll::Ready(None)`.
                false
            }
        }

        impl<T: $($where)*> Stream for $name<T> {
            type Item = T;

            fn poll_next(
                mut self: Pin<&mut Self>,
                cx: &mut Context<'_>,
            ) -> Poll<Option<Self::Item>> {
                if let Some(when) = self.peek_when() {
                    // The head of the queue changed, reset the deadline.
                    if self.sleep.deadline() != when {
                        self.sleep.as_mut().reset(when);
                    }

                    // Poll and wait for the next item to be ready.
                    if self.sleep.as_mut().poll(cx).is_ready() {
                        // Item is ready, yield it.
                        let value = self.pop_value().expect("pop after peek");
                        return Poll::Ready(Some(value));
                    }
                }

                Poll::Pending
            }
        }

        impl<T: $($where)*>  fmt::Debug for $name<T> where T: fmt::Debug {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let now = Instant::now();
                let mut f = f.debug_list();
                for (when, value) in self.iter() {
                    f.entry(&(when.saturating_duration_since(now), value));
                }
                f.finish()
            }
        }
    };
}

impl_queue!(ScheduledQueue, Sized);
impl_queue!(UniqueScheduledQueue, std::hash::Hash + Eq);

struct Item<T> {
    when: Instant,
    value: T,
}

impl<T> PartialEq for Item<T> {
    fn eq(&self, other: &Self) -> bool {
        other.when == self.when
    }
}
impl<T> Eq for Item<T> {}

impl<T> PartialOrd for Item<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T> Ord for Item<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.when.cmp(&other.when).reverse()
    }
}

impl<T> fmt::Debug for Item<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.value.fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use futures::StreamExt;

    use super::*;

    #[tokio::test(start_paused = true)]
    async fn test_scheduled_queue() {
        let mut s = ScheduledQueue::new();

        let start = Instant::now();

        s.schedule(start + Duration::from_millis(100), 4);
        s.schedule(start + Duration::from_millis(150), 5);

        s.schedule(start + Duration::from_nanos(3), 2);
        s.schedule(start + Duration::from_nanos(2), 2);
        s.schedule(start + Duration::from_nanos(1), 1);

        assert_eq!(s.len(), 5);
        assert_eq!(s.next().await, Some(1));
        assert_eq!(s.next().await, Some(2));
        assert_eq!(s.next().await, Some(2));

        // Schedule immediately!
        s.schedule(start, 3);

        assert_eq!(s.len(), 3);
        assert_eq!(s.next().await, Some(3));
        assert_eq!(s.next().await, Some(4));
        assert_eq!(s.next().await, Some(5));

        assert_eq!(s.len(), 0);
        assert!(s.is_empty());
    }

    #[tokio::test(start_paused = true)]
    async fn test_unique_scheduled_queue() {
        let mut s = UniqueScheduledQueue::new();

        let start = Instant::now();

        s.schedule(start, "xxx");
        s.schedule(start + Duration::from_nanos(1), "a");
        s.schedule(start + Duration::from_nanos(2), "b");
        s.schedule(start + Duration::from_millis(100), "c");
        s.schedule(start + Duration::from_millis(150), "d");
        s.schedule(start + Duration::from_millis(200), "e");

        assert_eq!(s.len(), 6);
        s.remove(&"xxx");
        assert_eq!(s.len(), 5);

        assert_eq!(s.next().await, Some("a"));
        assert_eq!(s.len(), 4);

        // Move `b` to the end.
        s.schedule(start + Duration::from_secs(1), "b");
        // Move `d` before `c`.
        s.schedule(start + Duration::from_millis(99), "d");
        // Immediately schedule a new element.
        s.schedule(start, "x");

        assert_eq!(s.len(), 5);
        assert_eq!(s.next().await, Some("x"));
        assert_eq!(s.next().await, Some("d"));
        assert_eq!(s.next().await, Some("c"));
        assert_eq!(s.next().await, Some("e"));
        assert_eq!(s.next().await, Some("b"));

        assert_eq!(s.len(), 0);
        assert!(s.is_empty());
    }
}