From b53a343ebe2e89a3806b75ec1e20b7220fc77a3c Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 23 Jan 2023 21:16:40 +0100 Subject: [PATCH] Improve startup time further by not using a reqwest client --- src/repository/mod.rs | 11 ++++++++++- src/web_api/mod.rs | 26 ++++++++------------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/repository/mod.rs b/src/repository/mod.rs index acc8bb4..a220f48 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -4,6 +4,7 @@ use std::{ str::FromStr, }; +use futures::future; use semver::{Version, VersionReq}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use tokio::{ @@ -111,10 +112,18 @@ impl Repository { &*BIN_DIR, &*NODE_VERSIONS_DIR, ]; - for dir in dirs { + for result in future::join_all(dirs.into_iter().map(|dir| async move { if !dir.exists() { fs::create_dir_all(dir).await.into_diagnostic()?; } + + Ok(()) + })) + .await + { + if let Err(e) = result { + return Err(e); + } } Ok(()) diff --git a/src/web_api/mod.rs b/src/web_api/mod.rs index beac43d..51e9e15 100644 --- a/src/web_api/mod.rs +++ b/src/web_api/mod.rs @@ -9,8 +9,6 @@ use crate::{ utils::progress_bar, }; -use reqwest::Client; - mod model; use futures_util::StreamExt; use miette::{miette, Context, IntoDiagnostic, Result}; @@ -23,7 +21,6 @@ mod test; #[derive(Clone, Debug)] pub struct WebApi { base_url: String, - client: Client, } impl Default for WebApi { @@ -37,17 +34,13 @@ impl WebApi { pub fn new(base_url: S) -> Self { Self { base_url: base_url.to_string(), - client: Client::new(), } } /// Returns the list of available node versions #[tracing::instrument(level = "debug")] pub async fn get_versions(&self) -> Result> { - let versions = self - .client - .get(format!("{}/index.json", self.base_url)) - .send() + let versions = reqwest::get(format!("{}/index.json", self.base_url)) .await .map_err(ReqwestError::from) .context("Fetching versions")? @@ -67,16 +60,13 @@ impl WebApi { version: S, writer: &mut W, ) -> Result { - let res = self - .client - .get(format!( - "{}/v{version}/node-v{version}{}", - self.base_url, *NODE_ARCHIVE_SUFFIX - )) - .send() - .await - .map_err(ReqwestError::from) - .context("Downloading nodejs")?; + let res = reqwest::get(format!( + "{}/v{version}/node-v{version}{}", + self.base_url, *NODE_ARCHIVE_SUFFIX + )) + .await + .map_err(ReqwestError::from) + .context("Downloading nodejs")?; let total_size = res .content_length()