Refactor binstalk-downloader APIs: Remove cancellation_future plus optimizations (#591)

- Refactor: Mv fn `utils::asyncify` into mod `utils`
 - Improve err msg for task failure in `utils::asyncify`
 - Make sure `asyncify` always returns the same annoymous type
   that implements `Future` if the `T` is same.
 - Rewrite `extract_bin` to avoid `block_in_place`
   support cancellation by dropping
 - Rm unused dep scopeguard from binstalk-downloader
 - Rewrite `extract_tar_based_stream` so that it is cancellable by dropping
 - Unbox `extract_future` in `async_extracter::extract_zip`
 - Refactor `Download` API: Remove `CancellationFuture` as param

   since all futures returned by `Download::and_*` does not call
   `block_in_place`, so they can be cancelled by drop instead of using this
   cumbersome hack.
 - Fix exports from mod `async_tar_visitor`
 - Make `signal::{ignore_signals, wait_on_cancellation_signal}` private
 - Rm the global variable `CANCELLED` in `wait_on_cancellation_signal`
   and rm fn `wait_on_cancellation_signal_inner`
 - Optimize `wait_on_cancellation_signal`: Avoid `tokio::select!` on `not(unix)`
 - Rm unnecessary `tokio::select!` in `wait_on_cancellation_signal` on unix
   Since `unix::wait_on_cancellation_signal_unix` already waits for ctrl + c signal.
 - Optimize `extract_bin`: Send `Bytes` to blocking thread for zero-copy
 - Optimize `extract_with_blocking_decoder`: Avoid dup monomorphization
 - Box fut of `fetch_crate_cratesio` in `PackageInfo::resolve`
 - Optimize `extract_zip_entry`: Spawn only one blocking task per fn call

   by using a mspc queue for the data to be written to the `outfile`.

   This would improve efficiency as using `tokio::fs::File` is expensive:
   It spawns a new blocking task, which needs one heap allocation and then
   pushed to a mpmc queue, and then wait for it to be done on every loop.

   This also fix a race condition where the unix permission is set before
   the whole file is written, which might be used by attackers.
 - Optimize `extract_zip`: Use one `BytesMut` for entire extraction process
   To avoid frequent allocation and deallocation.
 - Optimize `extract_zip_entry`: Inc prob of reusing alloc in `BytesMut`

   Performs the reserve before sending the buf over mpsc queue to
   increase the possibility of reusing the previous allocation.

   NOTE: `BytesMut` only reuses the previous allocation if it is the
   only one holds the reference to it, which is either on the first
   allocation or all the `Bytes` in the mpsc queue has been consumed,
   written to the file and dropped.

   Since reading from entry would have to wait for external file I/O,
   this would give the blocking thread some time to flush `Bytes`
   out.
 - Disable unused feature fs of dep tokio

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-12-12 14:15:30 +11:00 committed by GitHub
parent 058208bae9
commit db45f2fb7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 234 additions and 263 deletions

View file

@ -10,7 +10,6 @@ use crate::{
helpers::{
download::Download,
remote::{Client, Url},
signal::wait_on_cancellation_signal,
},
manifests::cargo_toml_binstall::{Meta, TarBasedFmt},
};
@ -54,10 +53,6 @@ pub async fn fetch_crate_cratesio(
let manifest_dir_path: PathBuf = format!("{name}-{version_name}").into();
Ok(Download::new(client, Url::parse(&crate_url)?)
.and_visit_tar(
TarBasedFmt::Tgz,
ManifestVisitor::new(manifest_dir_path),
Some(Box::pin(wait_on_cancellation_signal())),
)
.and_visit_tar(TarBasedFmt::Tgz, ManifestVisitor::new(manifest_dir_path))
.await?)
}

View file

@ -15,7 +15,6 @@ use crate::{
helpers::{
download::Download,
remote::{Client, Method},
signal::wait_on_cancellation_signal,
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
@ -167,7 +166,7 @@ impl super::Fetcher for GhCrateMeta {
let (url, pkg_fmt) = self.resolution.get().unwrap(); // find() is called first
debug!("Downloading package from: '{url}' dst:{dst:?} fmt:{pkg_fmt:?}");
Ok(Download::new(self.client.clone(), url.clone())
.and_extract(*pkg_fmt, dst, Some(Box::pin(wait_on_cancellation_signal())))
.and_extract(*pkg_fmt, dst)
.await?)
}

View file

@ -9,7 +9,6 @@ use crate::{
helpers::{
download::Download,
remote::{Client, Method},
signal::wait_on_cancellation_signal,
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
@ -72,11 +71,7 @@ impl super::Fetcher for QuickInstall {
let url = self.package_url();
debug!("Downloading package from: '{url}'");
Ok(Download::new(self.client.clone(), Url::parse(&url)?)
.and_extract(
self.pkg_fmt(),
dst,
Some(Box::pin(wait_on_cancellation_signal())),
)
.and_extract(self.pkg_fmt(), dst)
.await?)
}

View file

@ -3,7 +3,7 @@ use std::{future::pending, io};
use super::tasks::AutoAbortJoinHandle;
use crate::errors::BinstallError;
use tokio::{signal, sync::OnceCell};
use tokio::signal;
/// This function will poll the handle while listening for ctrl_c,
/// `SIGINT`, `SIGHUP`, `SIGTERM` and `SIGQUIT`.
@ -30,7 +30,7 @@ pub async fn cancel_on_user_sig_term<T>(
}
}
pub fn ignore_signals() -> io::Result<()> {
fn ignore_signals() -> io::Result<()> {
#[cfg(unix)]
unix::ignore_signals_on_unix()?;
@ -39,16 +39,7 @@ pub fn ignore_signals() -> io::Result<()> {
/// If call to it returns `Ok(())`, then all calls to this function after
/// that also returns `Ok(())`.
pub async fn wait_on_cancellation_signal() -> Result<(), io::Error> {
static CANCELLED: OnceCell<()> = OnceCell::const_new();
CANCELLED
.get_or_try_init(wait_on_cancellation_signal_inner)
.await
.copied()
}
async fn wait_on_cancellation_signal_inner() -> Result<(), io::Error> {
async fn wait_on_cancellation_signal() -> Result<(), io::Error> {
#[cfg(unix)]
async fn inner() -> Result<(), io::Error> {
unix::wait_on_cancellation_signal_unix().await
@ -56,16 +47,10 @@ async fn wait_on_cancellation_signal_inner() -> Result<(), io::Error> {
#[cfg(not(unix))]
async fn inner() -> Result<(), io::Error> {
// Use pending here so that tokio::select! would just skip this branch.
pending().await
signal::ctrl_c().await
}
tokio::select! {
biased;
res = signal::ctrl_c() => res,
res = inner() => res,
}
inner().await
}
#[cfg(unix)]

View file

@ -398,7 +398,15 @@ impl PackageInfo {
// Fetch crate via crates.io, git, or use a local manifest path
let manifest = match opts.manifest_path.as_ref() {
Some(manifest_path) => load_manifest_path(manifest_path)?,
None => fetch_crate_cratesio(client, crates_io_api_client, &name, &version_req).await?,
None => {
Box::pin(fetch_crate_cratesio(
client,
crates_io_api_client,
&name,
&version_req,
))
.await?
}
};
let Some(mut package) = manifest.package else {