-
Notifications
You must be signed in to change notification settings - Fork 628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fairness to futures_util::{select, try_select, select_ok} #2641
base: master
Are you sure you want to change the base?
Conversation
984689e
to
ceb1595
Compare
Hmm so I'm only just now realizing this repo has its own implementation of randomness. I'm willing to change to use that one, but I also like mine so I'm going to wait for a code reviewer to tell me what to do. |
|
@taiki-e Okay, this PR has been updated to omit |
FYI, these CI failures are not related to this PR. |
ec2bf60
to
685ac62
Compare
CI passes now! |
If we want to do this, can we also add deterministic (biased) versions of these combinators? In our codebase the randomized versions are undesirable as we want our execution to be completely deterministic. |
@goffrie There is select_biased. Does that meet your needs? |
No, because that's only usable in macro form and the combinator form is often simpler to use. |
@goffrie alright, though it's not too difficult to adapt it into combinator form outside of the pub async fn select_biased<Fut1, Fut2>(
mut a: Fut1,
mut b: Fut2,
) -> Either<Fut1::Output, Fut2::Output>
where
Fut1: FusedFuture + Unpin,
Fut2: FusedFuture + Unpin,
{
futures::select_biased! {
a_res = a => Either::Left(a_res),
b_res = b => Either::Right(b_res),
}
} |
I have resolved the merge conflicts. |
CI failures seem to be unrelated. |
@taiki-e just to clarify, who needs to make a decision on this? |
The maintainer needs to decide whether to accept this, and if so, whether to accept this API as is, whether to provide a biased variant, etc. Opinions and summaries on the advantages and disadvantages of these would be helpful in making a decision. |
Fairness by default is preferable in my opinion, because the alternative is a foot gun. In particular it is common to call So, in an unloaded system, both futures are processed as they become available, and in a loaded system, only one future is processed ever. Some may see this behavior as desirable, but in my opinion this is surprising enough that a bias in Now, should the biased variant be included? I don't really have a compelling reason to exclude it, especially because I've already authored the biased variant, and it ended up being pretty easy to maintain. It just forwards to a pre-existing macro. |
I pushed a commit which adds the biased variant as well as the tests for it. I didn't use my original implementation though, because I realized that from the standpoint of |
I realized I had two other functions I needed to write still, and that this code was getting repetitive fast. So I changed tactic and added a biased boolean to each of the future structs, and then branched on that if the std feature was enabled. |
e0f40c4
to
bc57a82
Compare
I have a stash where I converted this to rely on const generics in order to cut back on branching for the |
The most recent commit provides yet another option. Rather than using const generics, use a generic type with a default value of |
f73ee77
to
b2117dd
Compare
This PR has been updated to resolve the merge conflicts. |
Fixes #2135 by making these three functions fair, and then documenting that they are fair.