Reuse Client everywhere instead of recreate one

which pools the connection to the same site (github.com).

This commit also sets `USER_AGENT` so that quickinstall
can reuse it.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-01 00:03:43 +10:00
parent 9b26fea231
commit 50b436100e
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 9 additions and 11 deletions

View file

@ -7,13 +7,10 @@ use tokio::task::JoinHandle;
use url::Url; use url::Url;
use super::Data; use super::Data;
use crate::{ use crate::{download_and_extract, new_reqwest_client, remote_exists, BinstallError, PkgFmt};
download_and_extract, new_reqwest_client_builder, remote_exists, BinstallError, PkgFmt,
};
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download"; const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate"; const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
pub struct QuickInstall { pub struct QuickInstall {
package: String, package: String,
@ -91,9 +88,7 @@ impl QuickInstall {
let url = Url::parse(&stats_url)?; let url = Url::parse(&stats_url)?;
debug!("Sending installation report to quickinstall ({url})"); debug!("Sending installation report to quickinstall ({url})");
new_reqwest_client_builder() new_reqwest_client()?
.user_agent(USER_AGENT)
.build()?
.request(Method::HEAD, url.clone()) .request(Method::HEAD, url.clone())
.send() .send()
.await .await

View file

@ -59,8 +59,10 @@ pub fn load_manifest_path<P: AsRef<Path>>(
}) })
} }
pub fn new_reqwest_client_builder() -> ClientBuilder { fn new_reqwest_client_builder() -> ClientBuilder {
let mut builder = ClientBuilder::new(); const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
let mut builder = ClientBuilder::new().user_agent(USER_AGENT);
if let Some(ReqwestConfig { secure, min_tls }) = REQWESTGLOBALCONFIG.get() { if let Some(ReqwestConfig { secure, min_tls }) = REQWESTGLOBALCONFIG.get() {
if *secure { if *secure {
@ -77,8 +79,9 @@ pub fn new_reqwest_client_builder() -> ClientBuilder {
builder builder
} }
pub fn new_reqwest_client() -> reqwest::Result<Client> { pub fn new_reqwest_client() -> reqwest::Result<&'static Client> {
new_reqwest_client_builder().build() static CLIENT: OnceCell<Client> = OnceCell::new();
CLIENT.get_or_try_init(|| new_reqwest_client_builder().build())
} }
pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallError> { pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallError> {