Optimize use of tokio::select!: Use biased selection (#580)

as there is no need to randomize the first one to be polled.

For `cancel_on_user_sig_term` and `StreamReadable::fill_buf`, the
cancellation future should always to be polled first so that user would
feel responsive.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-11-30 14:05:52 +11:00 committed by GitHub
parent af82f1021c
commit ff737730f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -122,10 +122,12 @@ where
let option = self.handle.block_on(async {
if let Some(cancellation_future) = self.cancellation_future.as_mut() {
tokio::select! {
res = next_stream(&mut self.stream) => res,
biased;
res = cancellation_future => {
Err(res.err().unwrap_or_else(|| io::Error::from(DownloadError::UserAbort)))
},
res = next_stream(&mut self.stream) => res,
}
} else {
next_stream(&mut self.stream).await