From 52df530de43c47ae5e77dc702eedbae63ccebf19 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 12 Jan 2024 20:36:03 +0100 Subject: [PATCH] Reuse hyper::Client between downloads This avoids unnecessary TCP and TLS handshakes. --- src/download.rs | 12 ++++++++---- src/lib.rs | 8 +++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/download.rs b/src/download.rs index e5b1521..3b5fdb7 100644 --- a/src/download.rs +++ b/src/download.rs @@ -11,14 +11,18 @@ use hyper_rustls::TlsClient; use pbr::{ProgressBar, Units}; -pub fn download(remote_path: &str, local_path: &str) -> io::Result<()> { +pub fn download_client() -> Client { + let mut client = Client::with_connector(HttpsConnector::new(TlsClient::new())); + client.set_read_timeout(Some(Duration::new(5, 0))); + client.set_write_timeout(Some(Duration::new(5, 0))); + client +} + +pub fn download(client: &Client, remote_path: &str, local_path: &str) -> io::Result<()> { let mut stderr = stderr(); write!(stderr, "* Requesting {}\n", remote_path)?; - let mut client = Client::with_connector(HttpsConnector::new(TlsClient::new())); - client.set_read_timeout(Some(Duration::new(5, 0))); - client.set_write_timeout(Some(Duration::new(5, 0))); let mut response = match client.get(remote_path).send() { Ok(response) => response, Err(HyperError::Io(err)) => return Err(err), diff --git a/src/lib.rs b/src/lib.rs index 6349470..9d799f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ use std::path::Path; use std::str; pub use crate::database::{Database, PackageDepends}; -pub use crate::download::download; +pub use crate::download::{download, download_client}; pub use crate::package::Package; pub use crate::packagemeta::{PackageMeta, PackageMetaList}; @@ -19,6 +19,7 @@ mod packagemeta; pub struct Repo { local: String, remotes: Vec<String>, + client: hyper::Client, target: String, } @@ -60,7 +61,8 @@ impl Repo { Repo { local: format!("/tmp/pkg"), - remotes: remotes, + remotes, + client: download_client(), target: target.to_string(), } } @@ -78,7 +80,7 @@ impl Repo { )); for remote in self.remotes.iter() { let remote_path = format!("{}/{}/{}", remote, self.target, file); - res = download(&remote_path, &local_path).map(|_| local_path.clone()); + res = download(&self.client, &remote_path, &local_path).map(|_| local_path.clone()); if res.is_ok() { break; } -- GitLab