Skip to content
Snippets Groups Projects
Commit 52df530d authored by bjorn3's avatar bjorn3
Browse files

Reuse hyper::Client between downloads

This avoids unnecessary TCP and TLS handshakes.
parent 021f7937
No related branches found
No related tags found
1 merge request!43Reuse hyper::Client between downloads
......@@ -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),
......
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment