From 04195ca6975a77da15b55d0b2134b65b9a3c142c Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Sun, 12 Mar 2017 16:27:59 -0500 Subject: [PATCH] Introduce retry for both ygo/mtg searchers. --- Cargo.toml | 1 + src/lib.rs | 1 + src/searchers/mtg.rs | 22 +++++++++++++++++++++- src/searchers/yugioh.rs | 22 +++++++++++++++++++++- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f96fa41..b522d86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ select = "0.3.0" #reqwest = "0.4.0" hyper = "0.9.18" serde_json = "0.9" +retry = { git = "https://github.com/jimmycuadra/retry", rev = "3fa812e650d64ede61ea243fb83ef1a222ff0f84" } diff --git a/src/lib.rs b/src/lib.rs index e80ae5d..bbf7427 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ extern crate hyper; extern crate select; extern crate serde_json; +extern crate retry; pub mod searchers; diff --git a/src/searchers/mtg.rs b/src/searchers/mtg.rs index 9aedace..39c2254 100644 --- a/src/searchers/mtg.rs +++ b/src/searchers/mtg.rs @@ -7,11 +7,18 @@ use hyper::Client; use serde_json; use serde_json::Value; +use retry; +use retry::retry; +use retry::delay::Fixed; + use std; use std::io::Read; use std::any::Any; +const NUM_RETRIES: usize = 10; +const RETRY_WAIT_MILLIS: u64 = 500; + #[derive(Debug)] pub struct MtgCard { name: String, @@ -53,7 +60,10 @@ impl MtgSearcher { fn do_search (&self, name: &str) -> Result { let mut contents = String::new(); let api_url = &format!("https://api.magicthegathering.io/v1/cards?name={}", name); - self.client.get(api_url).send()?.read_to_string(&mut contents)?; + let mut response = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || { + self.client.get(api_url).send() + })?; + response.read_to_string(&mut contents)?; Result::Ok(contents) } } @@ -114,3 +124,13 @@ impl From for Error { Error::Json(error) } } + +impl From> for Error { + fn from (err: retry::Error) -> Error { + match err { + retry::Error::Operation { error, total_delay, tries } => { + Error::Http(error) + } + } + } +} diff --git a/src/searchers/yugioh.rs b/src/searchers/yugioh.rs index c96d69d..87abd12 100644 --- a/src/searchers/yugioh.rs +++ b/src/searchers/yugioh.rs @@ -7,11 +7,18 @@ use hyper::Client; use serde_json; use serde_json::Value; +use retry; +use retry::retry; +use retry::delay::Fixed; + use std; use std::io::Read; use std::any::Any; +const NUM_RETRIES: usize = 10; +const RETRY_WAIT_MILLIS: u64 = 500; + #[derive(Debug)] pub struct YugiohCard { name: String, @@ -54,7 +61,10 @@ impl YugiohSearcher { fn do_search (&self, name: &str) -> Result { let mut contents = String::new(); let api_url = &format!("http://yugiohprices.com/api/card_data/{}", name); - self.client.get(api_url).send()?.read_to_string(&mut contents)?; + let mut response = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || { + self.client.get(api_url).send() + })?; + response.read_to_string(&mut contents)?; Result::Ok(contents) } } @@ -114,3 +124,13 @@ impl From for Error { Error::Json(error) } } + +impl From> for Error { + fn from (err: retry::Error) -> Error { + match err { + retry::Error::Operation { error, total_delay, tries } => { + Error::Http(error) + } + } + } +}