cargo-binstall/crates/binstalk/src/fetchers/quickinstall.rs
Jiahao XU 87686cb2f7
Feature: Better retry policy in binstalk-downloader (#794)
Fixed #779 #791 

 - Retry request on timeout
 - Retry for `StatusCode::{REQUEST_TIMEOUT, GATEWAY_TIMEOUT}`
 - Add `DEFAULT_RETRY_DURATION_FOR_RATE_LIMIT` for 503/429
   if 503/429 does not give us a header or give us an invalid header on
   when to retry, we would default to
   `DEFAULT_RETRY_DURATION_FOR_RATE_LIMIT`.
 - Fix `Client::get_redirected_final_url`: Retry using `GET` on status code 400..405 + 410
 - Rename remote_exists => remote_gettable & support fallback to GET
   if HEAD fails due to status code 400..405 + 410.
 - Improve `Client::get_stream`: Include url & method in the err of the stream returned

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-02-13 13:43:48 +11:00

124 lines
3.4 KiB
Rust

use std::{path::Path, sync::Arc};
use compact_str::CompactString;
use tracing::{debug, warn};
use url::Url;
use crate::{
errors::BinstallError,
helpers::{download::Download, remote::Client, tasks::AutoAbortJoinHandle},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
};
use super::{Data, TargetData};
const BASE_URL: &str = "https://github.com/cargo-bins/cargo-quickinstall/releases/download";
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
pub struct QuickInstall {
client: Client,
package: String,
target_data: Arc<TargetData>,
}
#[async_trait::async_trait]
impl super::Fetcher for QuickInstall {
fn new(
client: Client,
data: Arc<Data>,
target_data: Arc<TargetData>,
) -> Arc<dyn super::Fetcher> {
let crate_name = &data.name;
let version = &data.version;
let target = &target_data.target;
Arc::new(Self {
client,
package: format!("{crate_name}-{version}-{target}"),
target_data,
})
}
fn find(self: Arc<Self>) -> AutoAbortJoinHandle<Result<bool, BinstallError>> {
AutoAbortJoinHandle::spawn(async move {
if cfg!(debug_assertions) {
debug!("Not sending quickinstall report in debug mode");
} else {
let this = self.clone();
tokio::spawn(async move {
if let Err(err) = this.report().await {
warn!(
"Failed to send quickinstall report for package {}: {err}",
this.package
)
}
});
}
let url = self.package_url();
debug!("Checking for package at: '{url}'");
Ok(self.client.remote_gettable(Url::parse(&url)?).await?)
})
}
async fn fetch_and_extract(&self, dst: &Path) -> Result<(), BinstallError> {
let url = self.package_url();
debug!("Downloading package from: '{url}'");
Ok(Download::new(self.client.clone(), Url::parse(&url)?)
.and_extract(self.pkg_fmt(), dst)
.await?)
}
fn pkg_fmt(&self) -> PkgFmt {
PkgFmt::Tgz
}
fn target_meta(&self) -> PkgMeta {
let mut meta = self.target_data.meta.clone();
meta.pkg_fmt = Some(self.pkg_fmt());
meta.bin_dir = Some("{ bin }{ binary-ext }".to_string());
meta
}
fn source_name(&self) -> CompactString {
CompactString::from("QuickInstall")
}
fn fetcher_name(&self) -> &'static str {
"QuickInstall"
}
fn is_third_party(&self) -> bool {
true
}
fn target(&self) -> &str {
&self.target_data.target
}
}
impl QuickInstall {
fn package_url(&self) -> String {
format!(
"{base_url}/{package}/{package}.tar.gz",
base_url = BASE_URL,
package = self.package
)
}
fn stats_url(&self) -> String {
format!(
"{stats_url}/{package}.tar.gz",
stats_url = STATS_URL,
package = self.package
)
}
pub async fn report(&self) -> Result<(), BinstallError> {
let url = Url::parse(&self.stats_url())?;
debug!("Sending installation report to quickinstall ({url})");
self.client.remote_gettable(url).await?;
Ok(())
}
}