Create new type LazyJobserverClient

and use it to replace `jobserver::Client`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-20 17:54:17 +10:00
parent a1d7a7c117
commit 3981400ebb
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 33 additions and 19 deletions

View file

@ -12,7 +12,7 @@ pub async fn install(
resolution: Resolution, resolution: Resolution,
opts: Arc<Options>, opts: Arc<Options>,
desired_targets: DesiredTargets, desired_targets: DesiredTargets,
jobserver_client: jobserver::Client, jobserver_client: LazyJobserverClient,
) -> Result<()> { ) -> Result<()> {
match resolution { match resolution {
Resolution::Fetch { Resolution::Fetch {
@ -140,8 +140,10 @@ async fn install_from_package(
async fn install_from_source( async fn install_from_source(
package: Package<Meta>, package: Package<Meta>,
target: &str, target: &str,
jobserver_client: jobserver::Client, lazy_jobserver_client: LazyJobserverClient,
) -> Result<()> { ) -> Result<()> {
let jobserver_client = lazy_jobserver_client.get().await?;
debug!( debug!(
"Running `cargo install {} --version {} --target {target}`", "Running `cargo install {} --version {} --target {target}`",
package.name, package.version package.name, package.version

View file

@ -1,20 +1,35 @@
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::sync::Arc;
use std::thread::available_parallelism; use std::thread::available_parallelism;
use jobserver::Client;
use tokio::sync::OnceCell;
use crate::BinstallError; use crate::BinstallError;
pub fn create_jobserver_client() -> Result<jobserver::Client, BinstallError> { #[derive(Clone)]
use jobserver::Client; pub struct LazyJobserverClient(Arc<OnceCell<Client>>);
impl LazyJobserverClient {
/// This must be called at the start of the program since
/// `Client::from_env` requires that.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
// Safety: // Safety:
// //
// Client::from_env is unsafe because from_raw_fd is unsafe. // Client::from_env is unsafe because from_raw_fd is unsafe.
// It doesn't do anything that is actually unsafe, like // It doesn't do anything that is actually unsafe, like
// dereferencing pointer. // dereferencing pointer.
if let Some(client) = unsafe { Client::from_env() } { let opt = unsafe { Client::from_env() };
Ok(client) Self(Arc::new(OnceCell::new_with(opt)))
} else { }
pub async fn get(&self) -> Result<&Client, BinstallError> {
self.0
.get_or_try_init(|| async {
let ncore = available_parallelism().map(NonZeroUsize::get).unwrap_or(1); let ncore = available_parallelism().map(NonZeroUsize::get).unwrap_or(1);
Ok(Client::new(ncore)?) Ok(Client::new(ncore)?)
})
.await
} }
} }

View file

@ -160,10 +160,7 @@ impl Termination for MainExit {
fn main() -> MainExit { fn main() -> MainExit {
// Create jobserver client // Create jobserver client
let jobserver_client = match create_jobserver_client() { let jobserver_client = LazyJobserverClient::new();
Ok(jobserver_client) => jobserver_client,
Err(binstall_err) => return MainExit::Error(binstall_err),
};
let start = Instant::now(); let start = Instant::now();
@ -184,7 +181,7 @@ fn main() -> MainExit {
}) })
} }
async fn entry(jobserver_client: jobserver::Client) -> Result<()> { async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
// Filter extraneous arg when invoked by cargo // Filter extraneous arg when invoked by cargo
// `cargo run -- --help` gives ["target/debug/cargo-binstall", "--help"] // `cargo run -- --help` gives ["target/debug/cargo-binstall", "--help"]
// `cargo binstall --help` gives ["/home/ryan/.cargo/bin/cargo-binstall", "binstall", "--help"] // `cargo binstall --help` gives ["/home/ryan/.cargo/bin/cargo-binstall", "binstall", "--help"]