From e3047c1bc967b3fab4d64ac559495e2ac3bd7d16 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 7 Aug 2018 22:26:53 +1000 Subject: [PATCH] Use HTTP client with timeout --- src/http.rs | 19 +++++++++++++------ src/sources/github/mod.rs | 5 +++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/http.rs b/src/http.rs index 7fc831f..a8edb67 100644 --- a/src/http.rs +++ b/src/http.rs @@ -4,14 +4,22 @@ use hyper::header::ContentLength; -use reqwest; - use std::io::Read; +use std::time::Duration; + +use reqwest::Client; + +/// Builds a customised HTTP client. +pub fn build_client() -> Result { + Client::builder() + .timeout(Duration::from_secs(5)) + .build() + .map_err(|x| format!("Unable to build cient: {:?}", x)) +} /// Downloads a text file from the specified URL. pub fn download_text(url: &str) -> Result { - // TODO: Decrease check time - let mut client = match reqwest::get(url) { + let mut client = match build_client()?.get(url).send() { Ok(v) => v, Err(v) => return Err(format!("Failed to GET resource: {:?}", v)), }; @@ -26,8 +34,7 @@ pub fn stream_file(url: &str, mut callback: F) -> Result<(), String> where F: FnMut(Vec, u64) -> (), { - // TODO: Decrease check time - let mut client = match reqwest::get(url) { + let mut client = match build_client()?.get(url).send() { Ok(v) => v, Err(v) => return Err(format!("Failed to GET resource: {:?}", v)), }; diff --git a/src/sources/github/mod.rs b/src/sources/github/mod.rs index a90dac0..1a8f0bd 100644 --- a/src/sources/github/mod.rs +++ b/src/sources/github/mod.rs @@ -2,7 +2,6 @@ //! //! Contains the Github API implementation of a release source. -use reqwest; use reqwest::header::UserAgent; use reqwest::StatusCode; @@ -10,6 +9,8 @@ use serde_json; use sources::types::*; +use http::build_client; + pub struct GithubReleases {} /// The configuration for this release. @@ -35,7 +36,7 @@ impl ReleaseSource for GithubReleases { let mut results: Vec = Vec::new(); // Build the HTTP client up - let client = reqwest::Client::new(); + let client = build_client()?; let mut response = client .get(&format!( "https://api.github.com/repos/{}/releases",