mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Spawn entry()
in main
to improve parallelism
Using `rt.block_on`, the future returned by `entry` can only be run on the main thread. Buf if we use `tokio::spawn`, then it can be run on any thread. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
7fa053cbd1
commit
b6245bcf4b
1 changed files with 16 additions and 7 deletions
23
src/main.rs
23
src/main.rs
|
@ -11,7 +11,7 @@ use miette::{miette, IntoDiagnostic, Result, WrapErr};
|
|||
use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
|
||||
use structopt::StructOpt;
|
||||
use tempfile::TempDir;
|
||||
use tokio::{process::Command, runtime::Runtime};
|
||||
use tokio::{process::Command, runtime::Runtime, task::JoinError};
|
||||
|
||||
use cargo_binstall::{
|
||||
bins,
|
||||
|
@ -84,6 +84,7 @@ enum MainExit {
|
|||
Success(Duration),
|
||||
Error(BinstallError),
|
||||
Report(miette::Report),
|
||||
JoinErr(JoinError),
|
||||
}
|
||||
|
||||
impl Termination for MainExit {
|
||||
|
@ -99,6 +100,11 @@ impl Termination for MainExit {
|
|||
eprintln!("{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
Self::JoinErr(err) => {
|
||||
error!("Fatal error:");
|
||||
eprintln!("{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,19 +113,22 @@ fn main() -> MainExit {
|
|||
let start = Instant::now();
|
||||
|
||||
let rt = Runtime::new().unwrap();
|
||||
let result = rt.block_on(entry());
|
||||
let handle = rt.spawn(entry());
|
||||
let result = rt.block_on(handle);
|
||||
drop(rt);
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
result
|
||||
.map(|_| MainExit::Success(done))
|
||||
.unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
.map(|res| {
|
||||
res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
.unwrap_or_else(MainExit::JoinErr)
|
||||
}
|
||||
|
||||
async fn entry() -> Result<()> {
|
||||
|
|
Loading…
Add table
Reference in a new issue