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
use crate::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use futures::{lock::Mutex, prelude::*};
use futures_timer::Delay;
use std::{
pin::Pin,
task::{Context, Poll},
time::Duration,
};
pub struct StatusSinks<T> {
inner: Mutex<Inner<T>>,
entries_tx: TracingUnboundedSender<YieldAfter<T>>,
}
struct Inner<T> {
entries: stream::FuturesUnordered<YieldAfter<T>>,
entries_rx: TracingUnboundedReceiver<YieldAfter<T>>,
}
struct YieldAfter<T> {
delay: Delay,
interval: Duration,
sender: Option<TracingUnboundedSender<T>>,
}
impl<T> Default for StatusSinks<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> StatusSinks<T> {
pub fn new() -> StatusSinks<T> {
let (entries_tx, entries_rx) = tracing_unbounded("status-sinks-entries");
StatusSinks {
inner: Mutex::new(Inner { entries: stream::FuturesUnordered::new(), entries_rx }),
entries_tx,
}
}
pub fn push(&self, interval: Duration, sender: TracingUnboundedSender<T>) {
let _ = self.entries_tx.unbounded_send(YieldAfter {
delay: Delay::new(interval),
interval,
sender: Some(sender),
});
}
pub async fn next(&self) -> ReadySinkEvent<'_, T> {
let mut inner = self.inner.lock().await;
let inner = &mut *inner;
loop {
let next_ready_entry = {
let entries = &mut inner.entries;
async move {
if let Some(v) = entries.next().await {
v
} else {
loop {
futures::pending!()
}
}
}
};
futures::select! {
new_entry = inner.entries_rx.next() => {
if let Some(new_entry) = new_entry {
inner.entries.push(new_entry);
}
},
(sender, interval) = next_ready_entry.fuse() => {
return ReadySinkEvent {
sinks: self,
sender: Some(sender),
interval,
}
}
}
}
}
}
#[must_use]
pub struct ReadySinkEvent<'a, T> {
sinks: &'a StatusSinks<T>,
sender: Option<TracingUnboundedSender<T>>,
interval: Duration,
}
impl<'a, T> ReadySinkEvent<'a, T> {
pub fn send(mut self, element: T) {
if let Some(sender) = self.sender.take() {
if sender.unbounded_send(element).is_ok() {
let _ = self.sinks.entries_tx.unbounded_send(YieldAfter {
delay: Delay::new(self.interval),
interval: self.interval,
sender: Some(sender),
});
}
}
}
}
impl<'a, T> Drop for ReadySinkEvent<'a, T> {
fn drop(&mut self) {
if let Some(sender) = self.sender.take() {
if sender.is_closed() {
return
}
let _ = self.sinks.entries_tx.unbounded_send(YieldAfter {
delay: Delay::new(self.interval),
interval: self.interval,
sender: Some(sender),
});
}
}
}
impl<T> futures::Future for YieldAfter<T> {
type Output = (TracingUnboundedSender<T>, Duration);
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = Pin::into_inner(self);
match Pin::new(&mut this.delay).poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(()) => {
let sender = this
.sender
.take()
.expect("sender is always Some unless the future is finished; qed");
Poll::Ready((sender, this.interval))
},
}
}
}
#[cfg(test)]
mod tests {
use super::StatusSinks;
use crate::mpsc::tracing_unbounded;
use futures::prelude::*;
use std::time::Duration;
#[test]
fn works() {
let status_sinks = StatusSinks::new();
let (tx, rx) = tracing_unbounded("test");
status_sinks.push(Duration::from_millis(100), tx);
let mut val_order = 5;
futures::executor::block_on(futures::future::select(
Box::pin(async move {
loop {
let ev = status_sinks.next().await;
val_order += 1;
ev.send(val_order);
}
}),
Box::pin(async {
let items: Vec<i32> = rx.take(3).collect().await;
assert_eq!(items, [6, 7, 8]);
}),
));
}
}