Disable tcp_nodelay for reqwest::Client and add rate limiting for https requests (#458)

This commit is contained in:
Jiahao XU 2022-10-07 15:51:34 +11:00 committed by GitHub
parent 8398ec2d4b
commit 76bc030f90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 243 additions and 99 deletions

View file

@ -4,7 +4,6 @@ use compact_str::{CompactString, ToCompactString};
use futures_util::stream::{FuturesUnordered, StreamExt};
use log::{debug, warn};
use once_cell::sync::OnceCell;
use reqwest::{Client, Method};
use serde::Serialize;
use strum::IntoEnumIterator;
use tinytemplate::TinyTemplate;
@ -14,7 +13,7 @@ use crate::{
errors::BinstallError,
helpers::{
download::Download,
remote::{get_redirected_final_url, remote_exists},
remote::{Client, Method},
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
@ -59,11 +58,9 @@ impl GhCrateMeta {
AutoAbortJoinHandle::spawn(async move {
debug!("Checking for package at: '{url}'");
Ok(
(remote_exists(client.clone(), url.clone(), Method::HEAD).await?
|| remote_exists(client, url.clone(), Method::GET).await?)
.then_some((url, pkg_fmt)),
)
Ok((client.remote_exists(url.clone(), Method::HEAD).await?
|| client.remote_exists(url.clone(), Method::GET).await?)
.then_some((url, pkg_fmt)))
})
})
}
@ -81,7 +78,11 @@ impl super::Fetcher for GhCrateMeta {
async fn find(&self) -> Result<bool, BinstallError> {
let repo = if let Some(repo) = self.data.repo.as_deref() {
Some(get_redirected_final_url(&self.client, Url::parse(repo)?).await?)
Some(
self.client
.get_redirected_final_url(Url::parse(repo)?)
.await?,
)
} else {
None
};

View file

@ -2,14 +2,15 @@ use std::{path::Path, sync::Arc};
use compact_str::CompactString;
use log::debug;
use reqwest::Client;
use reqwest::Method;
use tokio::task::JoinHandle;
use url::Url;
use crate::{
errors::BinstallError,
helpers::{download::Download, remote::remote_exists},
helpers::{
download::Download,
remote::{Client, Method},
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
};
@ -43,7 +44,9 @@ impl super::Fetcher for QuickInstall {
let url = self.package_url();
self.report();
debug!("Checking for package at: '{url}'");
remote_exists(self.client.clone(), Url::parse(&url)?, Method::HEAD).await
self.client
.remote_exists(Url::parse(&url)?, Method::HEAD)
.await
}
async fn fetch_and_extract(&self, dst: &Path) -> Result<(), BinstallError> {
@ -112,15 +115,7 @@ impl QuickInstall {
let url = Url::parse(&stats_url)?;
debug!("Sending installation report to quickinstall ({url})");
client
.request(Method::HEAD, url.clone())
.send()
.await
.map_err(|err| BinstallError::Http {
method: Method::HEAD,
url,
err,
})?;
client.remote_exists(url, Method::HEAD).await?;
Ok(())
})