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
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use futures::{
    stream::{FuturesOrdered, FuturesUnordered},
    Future, TryFutureExt, TryStreamExt,
};
use tokio::sync::{AcquireError, Semaphore, SemaphorePermit};

pub struct UnorderedPermit<'a, T: Future> {
    permit: SemaphorePermit<'a>,
    futures: &'a BoundedFuturesUnordered<T>,
}

/// An async-friendly FuturesUnordered of bounded size. In contrast to a bounded channel,
/// the queue makes it possible to modify and remove entries in it. In contrast to a FuturesUnordered,
/// the queue makes it possible to enforce a bound on the number of items in the queue.
pub struct BoundedFuturesUnordered<T: Future> {
    /// The maximum number of entries allowed in the queue
    capacity: usize,
    /// The actual items in the queue.
    queue: FuturesUnordered<T>,
    /// This semaphore has as many permits as there are empty spots in the
    /// `queue`, i.e., `capacity - queue.len()` many permits
    push_semaphore: Semaphore,
}

impl<T: Future> std::fmt::Debug for BoundedFuturesUnordered<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BoundedFuturesUnordered[cap: {}, free: {}]",
            self.capacity,
            self.push_semaphore.available_permits(),
        )
    }
}

unsafe impl<T: Future> Sync for BoundedFuturesUnordered<T> {}
unsafe impl<T: Future> Send for BoundedFuturesUnordered<T> {}

// We expect to grow this facade over time
impl<T: Future> BoundedFuturesUnordered<T> {
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            capacity,
            queue: FuturesUnordered::new(),
            push_semaphore: Semaphore::new(capacity),
        }
    }

    /// Push an item into the queue. If the queue is currently full this method
    /// blocks until an item is available
    pub async fn push(&self, item: T) {
        let permit = self.push_semaphore.acquire().await.unwrap();
        self.queue.push(item);
        permit.forget();
    }

    pub fn push_with_permit(&self, item: T, _permit: SemaphorePermit<'_>) {
        self.queue.push(item);
    }

    /// Waits for queue capacity. Once capacity to push one future is available, it is reserved for the caller.
    ///
    /// WARNING: the order of pushing to the queue is not guaranteed. It must be enforced by the caller.
    pub async fn reserve(&self) -> Result<UnorderedPermit<'_, T>, AcquireError> {
        let permit = self.push_semaphore.acquire().await?;
        Ok(UnorderedPermit {
            permit,
            futures: self,
        })
    }

    /// Report the available permits
    pub fn available_permits(&self) -> usize {
        self.push_semaphore.available_permits()
    }

    /// Report the length of the queue
    pub fn len(&self) -> usize {
        self.queue.len()
    }

    /// Report if  the queue is empty
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }
}

impl<U, V, T: Future<Output = Result<U, V>>> BoundedFuturesUnordered<T> {
    /// Creates a future that attempts to resolve the next item in the stream.
    /// If an error is encountered before the next item, the error is returned instead.
    pub fn try_next(&mut self) -> impl Future<Output = Result<Option<U>, V>> + '_ {
        self.queue.try_next().inspect_ok(|val| {
            if val.is_some() {
                self.push_semaphore.add_permits(1)
            }
        })
    }
}

impl<'a, T: Future> UnorderedPermit<'a, T> {
    /// Push an item using the reserved permit
    pub fn push(self, item: T) {
        self.futures.push_with_permit(item, self.permit);
    }
}

/// An async-friendly FuturesUnordered of bounded size. In contrast to a bounded channel,
/// the queue makes it possible to modify and remove entries in it. In contrast to a FuturesUnordered,
/// the queue makes it possible to enforce a bound on the number of items in the queue.
pub struct BoundedFuturesOrdered<T: Future> {
    /// The maximum number of entries allowed in the queue
    capacity: usize,
    /// The actual items in the queue. New items are appended at the back.
    queue: FuturesOrdered<T>,
    /// This semaphore has as many permits as there are empty spots in the
    /// `queue`, i.e., `capacity - queue.len()` many permits
    push_semaphore: Semaphore,
}

impl<T: Future> std::fmt::Debug for BoundedFuturesOrdered<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BoundedFuturesUnordered[cap: {}, queue: {}, push: {}]",
            self.capacity,
            self.queue.len(),
            self.push_semaphore.available_permits(),
        )
    }
}

unsafe impl<T: Future> Sync for BoundedFuturesOrdered<T> {}
unsafe impl<T: Future> Send for BoundedFuturesOrdered<T> {}

// We expect to grow this facade over time
impl<T: Future> BoundedFuturesOrdered<T> {
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            capacity,
            queue: FuturesOrdered::new(),
            push_semaphore: Semaphore::new(capacity),
        }
    }

    /// Push an item into the queue. If the queue is currently full this method
    /// blocks until an item is available
    pub async fn push(&mut self, item: T) {
        let permit = self.push_semaphore.acquire().await.unwrap();
        self.queue.push_back(item);
        permit.forget();
    }

    /// Report the available permits
    pub fn available_permits(&self) -> usize {
        self.push_semaphore.available_permits()
    }

    /// Report the length of the queue
    pub fn len(&self) -> usize {
        self.queue.len()
    }

    /// Report if  the queue is empty
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }
}

impl<U, V, T: Future<Output = Result<U, V>>> BoundedFuturesOrdered<T> {
    /// Creates a future that attempts to resolve the next item in the stream.
    /// If an error is encountered before the next item, the error is returned instead.
    pub fn try_next(&mut self) -> impl Future<Output = Result<Option<U>, V>> + '_ {
        self.queue.try_next().inspect_ok(|val| {
            if val.is_some() {
                self.push_semaphore.add_permits(1)
            }
        })
    }
}

#[cfg(test)]
mod tests {

    use super::{BoundedFuturesOrdered, BoundedFuturesUnordered};
    use futures::{future, FutureExt};

    #[tokio::test]
    async fn test_capacity_up() {
        let cap = 10;
        let futs = BoundedFuturesUnordered::with_capacity(cap);
        for i in 0..cap {
            futs.push(future::ready(i)).await;
            assert_eq!(futs.push_semaphore.available_permits(), cap - i - 1);
        }
        assert!(futs.push(future::ready(10)).now_or_never().is_none());
    }

    #[tokio::test]
    async fn test_reserve_up() {
        let cap = 10;
        let futs = BoundedFuturesUnordered::with_capacity(cap);
        let mut permits = Vec::new();
        // this forces the type of the future
        futs.push(future::ready(0)).await;
        for i in 1..cap {
            let permit = futs.reserve().await.unwrap();
            permits.push(permit);
            assert_eq!(futs.push_semaphore.available_permits(), cap - i - 1);
        }
        assert_eq!(futs.push_semaphore.available_permits(), 0);
        assert!(futs.push(future::ready(1)).now_or_never().is_none());
        drop(permits);
        assert_eq!(futs.push_semaphore.available_permits(), 9);
    }

    #[tokio::test]
    async fn test_reserve_down() {
        let cap = 10;
        let futs = BoundedFuturesUnordered::with_capacity(cap);
        let mut permits = Vec::new();
        // this forces the type of the future
        futs.push(future::ready(0)).await;
        for i in 1..cap {
            let permit = futs.reserve().await.unwrap();
            permits.push(permit);
            assert_eq!(futs.push_semaphore.available_permits(), cap - i - 1);
        }
        assert_eq!(futs.push_semaphore.available_permits(), 0);
        assert!(futs.push(future::ready(1)).now_or_never().is_none());
        for (i, permit) in permits.into_iter().enumerate() {
            permit.push(future::ready(i));
        }
        assert_eq!(futs.push_semaphore.available_permits(), 9);
    }

    #[tokio::test]
    async fn test_capacity_down() {
        let cap = 10;
        let mut futs = BoundedFuturesUnordered::with_capacity(cap);

        for i in 0..10 {
            futs.push(future::ready(Result::<usize, bool>::Ok(i))).await
        }
        for i in 0..10 {
            assert!(futs.try_next().await.unwrap().is_some());
            assert_eq!(futs.push_semaphore.available_permits(), i + 1)
        }
        assert!(futs.try_next().await.unwrap().is_none());
        assert_eq!(futs.push_semaphore.available_permits(), cap)
    }

    #[tokio::test]
    async fn test_capacity_up_ordered() {
        let cap = 10;
        let mut futs = BoundedFuturesOrdered::with_capacity(cap);
        for i in 0..cap {
            futs.push(future::ready(i)).await;
            assert_eq!(futs.push_semaphore.available_permits(), cap - i - 1);
        }
        assert!(futs.push(future::ready(10)).now_or_never().is_none());
    }

    #[tokio::test]
    async fn test_capacity_down_ordered() {
        let cap = 10;
        let mut futs = BoundedFuturesOrdered::with_capacity(cap);

        for i in 0..10 {
            futs.push(future::ready(Result::<usize, bool>::Ok(i))).await
        }
        for i in 0..10 {
            assert!(futs.try_next().await.unwrap().is_some());
            assert_eq!(futs.push_semaphore.available_permits(), i + 1)
        }
        assert!(futs.try_next().await.unwrap().is_none());
        assert_eq!(futs.push_semaphore.available_permits(), cap)
    }
}