mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-06 12:10:02 +00:00
Optimize GhCrateMeta::find
: Cache url resolution result (#810)
So that we don't have to create multiple HEAD/GET http request to get the final, redirected url. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
a8b9ae6fda
commit
2e118b3044
3 changed files with 75 additions and 50 deletions
|
@ -3,6 +3,8 @@ use std::{path::Path, sync::Arc};
|
|||
use compact_str::CompactString;
|
||||
pub use gh_crate_meta::*;
|
||||
pub use quickinstall::*;
|
||||
use tokio::sync::OnceCell;
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
errors::BinstallError,
|
||||
|
@ -60,9 +62,37 @@ pub trait Fetcher: Send + Sync {
|
|||
/// Data required to fetch a package
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Data {
|
||||
pub name: CompactString,
|
||||
pub version: CompactString,
|
||||
pub repo: Option<String>,
|
||||
name: CompactString,
|
||||
version: CompactString,
|
||||
repo: Option<String>,
|
||||
repo_final_url: OnceCell<Option<Url>>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub fn new(name: CompactString, version: CompactString, repo: Option<String>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
version,
|
||||
repo,
|
||||
repo_final_url: OnceCell::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn resolve_final_repo_url(&self, client: &Client) -> Result<&Option<Url>, BinstallError> {
|
||||
self.repo_final_url
|
||||
.get_or_try_init(move || {
|
||||
Box::pin(async move {
|
||||
if let Some(repo) = self.repo.as_deref() {
|
||||
Ok(Some(
|
||||
client.get_redirected_final_url(Url::parse(repo)?).await?,
|
||||
))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Target specific data required to fetch a package
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue