Crates.io | future-queue |
lib.rs | future-queue |
version | 0.3.0 |
source | src |
created_at | 2022-12-28 07:29:11.21513 |
updated_at | 2023-03-18 21:07:41.957957 |
description | Adapters to manage a queue of futures, where each future can have a different weight. |
homepage | |
repository | https://github.com/nextest-rs/future-queue |
max_upload_size | |
id | 746684 |
size | 85,188 |
future_queue
provides ways to run several futures:
This crate is part of the nextest organization on GitHub, and is designed to serve the needs of cargo-nextest.
Async programming in Rust often uses an adaptor called
buffer_unordered
:
this adaptor takes a stream of futures1, and executes all the futures limited to a maximum
amount of concurrency.
unordered
).Common use cases for buffer_unordered
include:
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, or that some subsets of futures should be mutually excluded from others.
For nextest in particular, some tests can be much heavier than others, and fewer of those tests should be run simultaneously. Also, some tests need to be mutually excluded from others, or other concurrency limits placed on them.
This crate provides two adaptors on streams.
future_queue
adaptorThe future_queue
adaptor can run several futures simultaneously,
limiting the concurrency to a maximum weight.
Rather than taking a stream of futures, this adaptor takes a stream of (usize, future)
pairs,
where the usize
indicates the weight of each future. This adaptor will schedule and buffer
futures to be run until queueing the next future will exceed the maximum weight.
Once all possible futures are scheduled, this adaptor will wait until some of the currently executing futures complete, and the current weight of running futures drops below the maximum weight, before scheduling new futures.
The weight of a future can be zero, in which case it doesn't count towards the maximum weight.
If all weights are 1, then future_queue
is exactly the same as buffer_unordered
.
use futures::{channel::oneshot, stream, StreamExt as _};
use future_queue::{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 queue = stream_of_futures.future_queue(10);
send_two.send("hello")?;
assert_eq!(queue.next().await, Some(Ok("hello")));
send_one.send("world")?;
assert_eq!(queue.next().await, Some(Ok("world")));
assert_eq!(queue.next().await, None);
future_queue_grouped
adaptorThe future_queue_grouped
adaptor is like future_queue
,
except it is possible to specify an optional group for each future. Each group has a maximum
weight, and a future will only be scheduled if both the maximum weight and the group weight
aren't exceeded.
The adaptor is as fair as possible under the given constraints: it will schedule futures in the order they're returned by the stream, without doing any reordering based on weight. When a future from a group completes, queued up futures in this group will be preferentially scheduled before any other futures from the provided stream.
Like with future_queue
:
use futures::{channel::oneshot, stream, StreamExt as _};
use future_queue::{StreamExt as _};
let (send_one, recv_one) = oneshot::channel();
let (send_two, recv_two) = oneshot::channel();
let stream_of_futures = stream::iter(
vec![
(1, Some("group1"), recv_one),
(2, None, recv_two),
],
);
let mut queue = stream_of_futures.future_queue_grouped(10, [("group1", 5)]);
send_two.send("hello")?;
assert_eq!(queue.next().await, Some(Ok("hello")));
send_one.send("world")?;
assert_eq!(queue.next().await, Some(Ok("world")));
assert_eq!(queue.next().await, None);
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.
This crate used to be called buffer-unordered-weighted
. It was renamed to future-queue
to be
more descriptive about what the crate does rather than how it's implemented.
See the CONTRIBUTING file for how to help out.
This project is available under the terms of either the Apache 2.0 license or the MIT license.
The code is derived from futures-rs, and is used under the Apache 2.0 and MIT licenses.
This adaptor takes a stream of futures for maximum generality. In practice this is often
an iterator of futures, converted over using
stream::iter
. ↩