sc_utils/
status_sinks.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
19use futures::{lock::Mutex, prelude::*};
20use futures_timer::Delay;
21use std::{
22	pin::Pin,
23	task::{Context, Poll},
24	time::Duration,
25};
26
27/// Holds a list of `UnboundedSender`s, each associated with a certain time period. Every time the
28/// period elapses, we push an element on the sender.
29///
30/// Senders are removed only when they are closed.
31pub struct StatusSinks<T> {
32	/// Should only be locked by `next`.
33	inner: Mutex<Inner<T>>,
34	/// Sending side of `Inner::entries_rx`.
35	entries_tx: TracingUnboundedSender<YieldAfter<T>>,
36}
37
38struct Inner<T> {
39	/// The actual entries of the list.
40	entries: stream::FuturesUnordered<YieldAfter<T>>,
41	/// Receives new entries and puts them in `entries`.
42	entries_rx: TracingUnboundedReceiver<YieldAfter<T>>,
43}
44
45struct YieldAfter<T> {
46	delay: Delay,
47	interval: Duration,
48	sender: Option<TracingUnboundedSender<T>>,
49}
50
51impl<T> Default for StatusSinks<T> {
52	fn default() -> Self {
53		Self::new()
54	}
55}
56
57impl<T> StatusSinks<T> {
58	/// Builds a new empty collection.
59	pub fn new() -> StatusSinks<T> {
60		let (entries_tx, entries_rx) = tracing_unbounded("status-sinks-entries", 100_000);
61
62		StatusSinks {
63			inner: Mutex::new(Inner { entries: stream::FuturesUnordered::new(), entries_rx }),
64			entries_tx,
65		}
66	}
67
68	/// Adds a sender to the collection.
69	///
70	/// The `interval` is the time period between two pushes on the sender.
71	pub fn push(&self, interval: Duration, sender: TracingUnboundedSender<T>) {
72		let _ = self.entries_tx.unbounded_send(YieldAfter {
73			delay: Delay::new(interval),
74			interval,
75			sender: Some(sender),
76		});
77	}
78
79	/// Waits until one of the sinks is ready, then returns an object that can be used to send
80	/// an element on said sink.
81	///
82	/// If the object isn't used to send an element, the slot is skipped.
83	pub async fn next(&self) -> ReadySinkEvent<'_, T> {
84		// This is only ever locked by `next`, which means that one `next` at a time can run.
85		let mut inner = self.inner.lock().await;
86		let inner = &mut *inner;
87
88		loop {
89			// Future that produces the next ready entry in `entries`, or doesn't produce anything
90			// if the list is empty.
91			let next_ready_entry = {
92				let entries = &mut inner.entries;
93				async move {
94					if let Some(v) = entries.next().await {
95						v
96					} else {
97						loop {
98							futures::pending!()
99						}
100					}
101				}
102			};
103
104			futures::select! {
105				new_entry = inner.entries_rx.next() => {
106					if let Some(new_entry) = new_entry {
107						inner.entries.push(new_entry);
108					}
109				},
110				(sender, interval) = next_ready_entry.fuse() => {
111					return ReadySinkEvent {
112						sinks: self,
113						sender: Some(sender),
114						interval,
115					}
116				}
117			}
118		}
119	}
120}
121
122/// One of the sinks is ready.
123#[must_use]
124pub struct ReadySinkEvent<'a, T> {
125	sinks: &'a StatusSinks<T>,
126	sender: Option<TracingUnboundedSender<T>>,
127	interval: Duration,
128}
129
130impl<'a, T> ReadySinkEvent<'a, T> {
131	/// Sends an element on the sender.
132	pub fn send(mut self, element: T) {
133		if let Some(sender) = self.sender.take() {
134			if sender.unbounded_send(element).is_ok() {
135				let _ = self.sinks.entries_tx.unbounded_send(YieldAfter {
136					// Note that since there's a small delay between the moment a task is
137					// woken up and the moment it is polled, the period is actually not
138					// `interval` but `interval + <delay>`. We ignore this problem in
139					// practice.
140					delay: Delay::new(self.interval),
141					interval: self.interval,
142					sender: Some(sender),
143				});
144			}
145		}
146	}
147}
148
149impl<'a, T> Drop for ReadySinkEvent<'a, T> {
150	fn drop(&mut self) {
151		if let Some(sender) = self.sender.take() {
152			if sender.is_closed() {
153				return
154			}
155
156			let _ = self.sinks.entries_tx.unbounded_send(YieldAfter {
157				delay: Delay::new(self.interval),
158				interval: self.interval,
159				sender: Some(sender),
160			});
161		}
162	}
163}
164
165impl<T> futures::Future for YieldAfter<T> {
166	type Output = (TracingUnboundedSender<T>, Duration);
167
168	fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
169		let this = Pin::into_inner(self);
170
171		match Pin::new(&mut this.delay).poll(cx) {
172			Poll::Pending => Poll::Pending,
173			Poll::Ready(()) => {
174				let sender = this
175					.sender
176					.take()
177					.expect("sender is always Some unless the future is finished; qed");
178				Poll::Ready((sender, this.interval))
179			},
180		}
181	}
182}
183
184#[cfg(test)]
185mod tests {
186	use super::StatusSinks;
187	use crate::mpsc::tracing_unbounded;
188	use futures::prelude::*;
189	use std::time::Duration;
190
191	#[test]
192	fn works() {
193		// We're not testing that the `StatusSink` properly enforces an order in the intervals, as
194		// this easily causes test failures on busy CPUs.
195
196		let status_sinks = StatusSinks::new();
197
198		let (tx, rx) = tracing_unbounded("test", 100_000);
199		status_sinks.push(Duration::from_millis(100), tx);
200
201		let mut val_order = 5;
202
203		futures::executor::block_on(futures::future::select(
204			Box::pin(async move {
205				loop {
206					let ev = status_sinks.next().await;
207					val_order += 1;
208					ev.send(val_order);
209				}
210			}),
211			Box::pin(async {
212				let items: Vec<i32> = rx.take(3).collect().await;
213				assert_eq!(items, [6, 7, 8]);
214			}),
215		));
216	}
217}