mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-25 23:00:03 +00:00
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:
parent
21eac33e1f
commit
f09004b5b7
3 changed files with 23 additions and 17 deletions
|
@ -72,6 +72,7 @@ impl Resolution {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn resolve(
|
||||
opts: Arc<Options>,
|
||||
crate_name: CrateName,
|
||||
|
@ -80,6 +81,7 @@ pub async fn resolve(
|
|||
temp_dir: Arc<Path>,
|
||||
install_path: Arc<Path>,
|
||||
client: Client,
|
||||
crates_io_api_client: crates_io_api::AsyncClient,
|
||||
) -> Result<Resolution> {
|
||||
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)
|
||||
let manifest = match opts.manifest_path.clone() {
|
||||
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();
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use cargo_toml::Manifest;
|
||||
use crates_io_api::AsyncClient;
|
||||
|
@ -18,22 +17,15 @@ use visitor::ManifestVisitor;
|
|||
/// Fetch a crate Cargo.toml by name and version from crates.io
|
||||
pub async fn fetch_crate_cratesio(
|
||||
client: &Client,
|
||||
crates_io_api_client: &AsyncClient,
|
||||
name: &str,
|
||||
version_req: &str,
|
||||
) -> Result<Manifest<Meta>, BinstallError> {
|
||||
// Fetch / update index
|
||||
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
|
||||
let base_info =
|
||||
api_client
|
||||
let base_info = crates_io_api_client
|
||||
.get_crate(name.as_ref())
|
||||
.await
|
||||
.map_err(|err| BinstallError::CratesIoApi {
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -216,6 +216,13 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
|||
// Initialize reqwest client
|
||||
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
|
||||
let mut log_config = ConfigBuilder::new();
|
||||
log_config.add_filter_ignore("hyper".to_string());
|
||||
|
@ -273,6 +280,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
|||
temp_dir_path.clone(),
|
||||
install_path.clone(),
|
||||
client.clone(),
|
||||
crates_io_api_client.clone(),
|
||||
))
|
||||
})
|
||||
.collect();
|
||||
|
@ -308,6 +316,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
|||
let jobserver_client = jobserver_client.clone();
|
||||
let desired_targets = desired_targets.clone();
|
||||
let client = client.clone();
|
||||
let crates_io_api_client = crates_io_api_client.clone();
|
||||
let cli_overrides = cli_overrides.clone();
|
||||
let install_path = install_path.clone();
|
||||
|
||||
|
@ -320,6 +329,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
|||
temp_dir_path,
|
||||
install_path,
|
||||
client,
|
||||
crates_io_api_client,
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue