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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) The buffer-unordered-weighted Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! `buffer_unordered_weighted` is a variant of
//! [`buffer_unordered`](https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html#method.buffer_unordered),
//! where each future can be assigned a different weight.
//!
//! This crate is part of the [nextest organization](https://github.com/nextest-rs) on GitHub, and is
//! designed to serve the needs of cargo-nextest.
//!
//! # Motivation
//!
//! Async programming in Rust often uses an adaptor called `buffer_unordered`: this adaptor takes a
//! stream of futures[^1], and executes all the futures limited to a maximum amount of concurrency.
//!
//! * Futures are started in the order the stream returns them in.
//! * Once started, futures are polled simultaneously, and completed future outputs are returned
//!   in arbitrary order (hence the `unordered`).
//!
//! Common use cases for `buffer_unordered` include:
//!
//! * Sending network requests concurrently, but limiting the amount of concurrency to avoid
//!   overwhelming the remote server.
//! * Running tests with a tool like [cargo-nextest](https://nexte.st).
//!
//! `buffer_unordered` works well for many use cases. However, one issue with it is that it treats
//! all futures as equally taxing: there's no way to say that some futures consume more resources
//! than others. For nextest in particular, some tests can be much heavier-weight than others, and
//! fewer of those tests should be run simultaneously.
//!
//! # About this crate
//!
//! This crate provides an adapter on streams called `buffer_unordered_weighted`, which can run
//! several futures simultaneously, limiting the concurrency to a maximum *weight*. Rather than taking a
//! stream of futures, it takes a stream of `(usize, future)` pairs, where the `usize` indicates the
//! weight of each future. This adapter will buffer futures until the maximum weight is exceeded.
//! Further futures will only be run after the current weight of running futures drops below the maximum
//! weight.
//!
//! Note that in some cases, the current weight may exceed the maximum weight. For example, let's say
//! the maximum weight is 24, and the current weight is 20. If the next future has weight 5, then it
//! will be buffered and the current weight will become 25. No further futures will be buffered until
//! the current weight falls to 23 or below. (It is possible to always stay below the limit and hold the
//! next future in abeyance; however, that complicates the implementation a little and is also not the
//! behavior desired by nextest. An adapter to do so may be provided in the future.)
//!
//! The weight of a future can even be zero, in which case it doesn't count towards the maximum
//! weight.
//!
//! If all weights are 1, then `buffer_unordered_weighted` is exactly the same as `buffer_unordered`.
//!
//! # Examples
//!
//! ```
//! # futures::executor::block_on(async {
//! use futures::{channel::oneshot, stream, StreamExt as _};
//! use buffer_unordered_weighted::{StreamExt as _};
//!
//! let (send_one, recv_one) = oneshot::channel();
//! let (send_two, recv_two) = oneshot::channel();
//!
//! let stream_of_futures = stream::iter(vec![(1, recv_one), (2, recv_two)]);
//! let mut buffered = stream_of_futures.buffer_unordered_weighted(10);
//!
//! send_two.send("hello")?;
//! assert_eq!(buffered.next().await, Some(Ok("hello")));
//!
//! send_one.send("world")?;
//! assert_eq!(buffered.next().await, Some(Ok("world")));
//!
//! assert_eq!(buffered.next().await, None);
//! # Ok::<(), &'static str>(()) }).unwrap();
//! ```
//!
//! # Minimum supported Rust version (MSRV)
//!
//! The minimum supported Rust version is **Rust 1.56.**
//!
//! The MSRV will likely not change in the medium term, but while this crate is a pre-release
//! (0.x.x) it may have its MSRV bumped in a patch release. Once this crate has reached 1.x, any
//! MSRV bump will be accompanied with a new minor version.
//!
//! [^1]: This adaptor takes a stream of futures for maximum generality. In practice this is often
//!     an *iterator* of futures, converted over using
//!     [`stream::iter`](https://docs.rs/futures/latest/futures/stream/fn.iter.html).
//!

use futures_util::{
    stream::{Fuse, FuturesUnordered},
    Future, Stream, StreamExt as _,
};
use pin_project_lite::pin_project;
use private::WeightedFuture;
use std::{
    fmt,
    pin::Pin,
    task::{Context, Poll},
};

impl<T: ?Sized> StreamExt for T where T: Stream {}

/// An extension trait for `Stream`s that provides
/// [`buffer_unordered_weighted`](StreamExt::buffer_unordered_weighted).
pub trait StreamExt: Stream {
    /// An adaptor for creating a buffered list of pending futures (unordered), where
    /// each future has a different weight.
    ///
    /// This stream must return values of type `(usize, impl Future)`, where the `usize` indicates
    /// the weight of each future. This adaptor will buffer futures up to weight `max_weight`, and
    /// then return the outputs in the order in which they complete.
    ///
    /// The weight may be exceeded if the last future to be queued has a weight greater than
    /// `max_weight` minus the total weight of currently executing futures. However, no further
    /// futures will be queued until the total weights of running futures falls below `max_weight`.
    ///
    /// The adaptor will buffer futures in the order they're returned by the stream, without doing
    /// any reordering based on weight.
    ///
    /// The weight of a future can be 0, in which case it will not count towards the total weight.
    ///
    /// The returned stream will be a stream of each future's output.
    ///
    /// # Examples
    ///
    /// See [the crate documentation](crate#examples) for an example.
    ///
    fn buffer_unordered_weighted<Fut>(self, max_weight: usize) -> BufferUnorderedWeighted<Self>
    where
        Self: Sized + Stream<Item = (usize, Fut)>,
        Fut: Future,
    {
        assert_stream::<Fut::Output, _>(BufferUnorderedWeighted::new(self, max_weight))
    }
}

pin_project! {
    /// Stream for the [`buffer_unordered_weighted`](StreamExt::buffer_unordered_weighted) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct BufferUnorderedWeighted<St>
    where
        St: Stream,
        St::Item: WeightedFuture,
     {
        #[pin]
        stream: Fuse<St>,
        in_progress_queue: FuturesUnordered<FutureWithWeight<<St::Item as WeightedFuture>::Future>>,
        max_weight: usize,
        current_weight: usize,
    }
}

impl<St> fmt::Debug for BufferUnorderedWeighted<St>
where
    St: Stream + fmt::Debug,
    St::Item: WeightedFuture,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BufferUnorderedWeighted")
            .field("stream", &self.stream)
            .field("in_progress_queue", &self.in_progress_queue)
            .field("max_weight", &self.max_weight)
            .field("current_weight", &self.current_weight)
            .finish()
    }
}

impl<St> BufferUnorderedWeighted<St>
where
    St: Stream,
    St::Item: WeightedFuture,
{
    pub(crate) fn new(stream: St, max_weight: usize) -> Self {
        Self {
            stream: stream.fuse(),
            in_progress_queue: FuturesUnordered::new(),
            max_weight,
            current_weight: 0,
        }
    }

    /// Returns the maximum weight of futures allowed to be run by this adaptor.
    pub fn max_weight(&self) -> usize {
        self.max_weight
    }

    /// Returns the currently running weight of futures.
    pub fn current_weight(&self) -> usize {
        self.current_weight
    }

    /// Acquires a reference to the underlying sink or stream that this combinator is
    /// pulling from.
    pub fn get_ref(&self) -> &St {
        self.stream.get_ref()
    }

    /// Acquires a mutable reference to the underlying sink or stream that this
    /// combinator is pulling from.
    ///
    /// Note that care must be taken to avoid tampering with the state of the
    /// sink or stream which may otherwise confuse this combinator.
    pub fn get_mut(&mut self) -> &mut St {
        self.stream.get_mut()
    }

    /// Acquires a pinned mutable reference to the underlying sink or stream that this
    /// combinator is pulling from.
    ///
    /// Note that care must be taken to avoid tampering with the state of the
    /// sink or stream which may otherwise confuse this combinator.
    pub fn get_pin_mut(self: Pin<&mut Self>) -> core::pin::Pin<&mut St> {
        self.project().stream.get_pin_mut()
    }

    /// Consumes this combinator, returning the underlying sink or stream.
    ///
    /// Note that this may discard intermediate state of this combinator, so
    /// care should be taken to avoid losing resources when this is called.
    pub fn into_inner(self) -> St {
        self.stream.into_inner()
    }
}

impl<St> Stream for BufferUnorderedWeighted<St>
where
    St: Stream,
    St::Item: WeightedFuture,
{
    type Item = <<St::Item as WeightedFuture>::Future as Future>::Output;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();

        // First up, try to spawn off as many futures as possible by filling up
        // our queue of futures.
        while *this.current_weight < *this.max_weight {
            match this.stream.as_mut().poll_next(cx) {
                Poll::Ready(Some(weighted_future)) => {
                    let (weight, future) = weighted_future.into_components();
                    *this.current_weight =
                        this.current_weight.checked_add(weight).unwrap_or_else(|| {
                            panic!(
                                "buffer_unordered_weighted: added weight {} to current {}, overflowed",
                                weight,
                                this.current_weight,
                            )
                        });
                    this.in_progress_queue
                        .push(FutureWithWeight::new(weight, future));
                }
                Poll::Ready(None) | Poll::Pending => break,
            }
        }

        // Attempt to pull the next value from the in_progress_queue
        match this.in_progress_queue.poll_next_unpin(cx) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Some((weight, output))) => {
                *this.current_weight = this.current_weight.checked_sub(weight).unwrap_or_else(|| {
                    panic!(
                        "buffer_unordered_weighted: subtracted weight {} from current {}, overflowed",
                        weight,
                        this.current_weight,
                    )
                });
                return Poll::Ready(Some(output));
            }
            Poll::Ready(None) => {}
        }

        // If more values are still coming from the stream, we're not done yet
        if this.stream.is_done() {
            Poll::Ready(None)
        } else {
            Poll::Pending
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let queue_len = self.in_progress_queue.len();
        let (lower, upper) = self.stream.size_hint();
        let lower = lower.saturating_add(queue_len);
        let upper = match upper {
            Some(x) => x.checked_add(queue_len),
            None => None,
        };
        (lower, upper)
    }
}

mod private {
    use futures_util::Future;

    pub trait WeightedFuture {
        type Future: Future;

        fn into_components(self) -> (usize, Self::Future);
    }

    impl<Fut> WeightedFuture for (usize, Fut)
    where
        Fut: Future,
    {
        type Future = Fut;

        #[inline]
        fn into_components(self) -> (usize, Self::Future) {
            self
        }
    }
}

pin_project! {
    #[must_use = "futures do nothing unless polled"]
    struct FutureWithWeight<Fut> {
        #[pin]
        future: Fut,
        weight: usize,
    }
}

impl<Fut> FutureWithWeight<Fut> {
    pub fn new(weight: usize, future: Fut) -> Self {
        Self { future, weight }
    }
}

impl<Fut> Future for FutureWithWeight<Fut>
where
    Fut: Future,
{
    type Output = (usize, Fut::Output);
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();

        match this.future.poll(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(output) => Poll::Ready((*this.weight, output)),
        }
    }
}

pub(crate) fn assert_stream<T, S>(stream: S) -> S
where
    S: Stream<Item = T>,
{
    stream
}