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:
Jiahao XU 2023-02-16 12:51:47 +11:00 committed by GitHub
parent a8b9ae6fda
commit 2e118b3044
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 50 deletions

View file

@ -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