Optimize: Share crates_io_api::AsyncClient

So that the connection pool and the rate limit will be shared.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-21 14:50:51 +10:00
parent 21eac33e1f
commit f09004b5b7
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 23 additions and 17 deletions

View file

@ -72,6 +72,7 @@ impl Resolution {
} }
} }
#[allow(clippy::too_many_arguments)]
pub async fn resolve( pub async fn resolve(
opts: Arc<Options>, opts: Arc<Options>,
crate_name: CrateName, crate_name: CrateName,
@ -80,6 +81,7 @@ pub async fn resolve(
temp_dir: Arc<Path>, temp_dir: Arc<Path>,
install_path: Arc<Path>, install_path: Arc<Path>,
client: Client, client: Client,
crates_io_api_client: crates_io_api::AsyncClient,
) -> Result<Resolution> { ) -> Result<Resolution> {
info!("Installing package: '{}'", crate_name); info!("Installing package: '{}'", crate_name);
@ -105,7 +107,9 @@ pub async fn resolve(
// TODO: support git-based fetches (whole repo name rather than just crate name) // TODO: support git-based fetches (whole repo name rather than just crate name)
let manifest = match opts.manifest_path.clone() { let manifest = match opts.manifest_path.clone() {
Some(manifest_path) => load_manifest_path(manifest_path.join("Cargo.toml"))?, Some(manifest_path) => load_manifest_path(manifest_path.join("Cargo.toml"))?,
None => fetch_crate_cratesio(&client, &crate_name.name, &version).await?, None => {
fetch_crate_cratesio(&client, &crates_io_api_client, &crate_name.name, &version).await?
}
}; };
let package = manifest.package.unwrap(); let package = manifest.package.unwrap();

View file

@ -1,5 +1,4 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Duration;
use cargo_toml::Manifest; use cargo_toml::Manifest;
use crates_io_api::AsyncClient; use crates_io_api::AsyncClient;
@ -18,22 +17,15 @@ use visitor::ManifestVisitor;
/// Fetch a crate Cargo.toml by name and version from crates.io /// Fetch a crate Cargo.toml by name and version from crates.io
pub async fn fetch_crate_cratesio( pub async fn fetch_crate_cratesio(
client: &Client, client: &Client,
crates_io_api_client: &AsyncClient,
name: &str, name: &str,
version_req: &str, version_req: &str,
) -> Result<Manifest<Meta>, BinstallError> { ) -> Result<Manifest<Meta>, BinstallError> {
// Fetch / update index // Fetch / update index
debug!("Looking up crate information"); debug!("Looking up crate information");
// Build crates.io api client
let api_client = AsyncClient::new(
"cargo-binstall (https://github.com/ryankurte/cargo-binstall)",
Duration::from_millis(100),
)
.expect("bug: invalid user agent");
// Fetch online crate information // Fetch online crate information
let base_info = let base_info = crates_io_api_client
api_client
.get_crate(name.as_ref()) .get_crate(name.as_ref())
.await .await
.map_err(|err| BinstallError::CratesIoApi { .map_err(|err| BinstallError::CratesIoApi {

View file

@ -216,6 +216,13 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
// Initialize reqwest client // Initialize reqwest client
let client = create_reqwest_client(opts.secure, opts.min_tls_version.map(|v| v.into()))?; let client = create_reqwest_client(opts.secure, opts.min_tls_version.map(|v| v.into()))?;
// Build crates.io api client
let crates_io_api_client = crates_io_api::AsyncClient::new(
"cargo-binstall (https://github.com/ryankurte/cargo-binstall)",
Duration::from_millis(100),
)
.expect("bug: invalid user agent");
// Setup logging // Setup logging
let mut log_config = ConfigBuilder::new(); let mut log_config = ConfigBuilder::new();
log_config.add_filter_ignore("hyper".to_string()); log_config.add_filter_ignore("hyper".to_string());
@ -273,6 +280,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
temp_dir_path.clone(), temp_dir_path.clone(),
install_path.clone(), install_path.clone(),
client.clone(), client.clone(),
crates_io_api_client.clone(),
)) ))
}) })
.collect(); .collect();
@ -308,6 +316,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
let jobserver_client = jobserver_client.clone(); let jobserver_client = jobserver_client.clone();
let desired_targets = desired_targets.clone(); let desired_targets = desired_targets.clone();
let client = client.clone(); let client = client.clone();
let crates_io_api_client = crates_io_api_client.clone();
let cli_overrides = cli_overrides.clone(); let cli_overrides = cli_overrides.clone();
let install_path = install_path.clone(); let install_path = install_path.clone();
@ -320,6 +329,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
temp_dir_path, temp_dir_path,
install_path, install_path,
client, client,
crates_io_api_client,
) )
.await?; .await?;