Use retry crate to retry http requests, since the hyper connection may be closed when we try to use it.

This commit is contained in:
Adrian Malacoda 2017-03-12 15:49:16 -05:00
parent 6c9adc161f
commit 89e3d91f54
4 changed files with 32 additions and 3 deletions

View File

@ -9,3 +9,4 @@ env_logger = "0.4.0"
lru-cache = "0.1.0"
select = "0.3.0"
hyper = "0.10.4"
retry = { git = "https://github.com/jimmycuadra/retry", rev = "3fa812e650d64ede61ea243fb83ef1a222ff0f84" }

View File

@ -1,16 +1,22 @@
extern crate lru_cache;
extern crate hyper;
extern crate select;
extern crate retry;
pub mod pirates;
pub mod ninjas;
mod parser;
//mod retry;
#[macro_use]
extern crate log;
#[derive(Debug)]
pub enum Error {
Http(hyper::error::Error),
Io(std::io::Error),
Parse(String)
Parse(String),
Unknown(String)
}
impl From<hyper::error::Error> for Error {
@ -31,6 +37,16 @@ impl From<std::num::ParseIntError> for Error {
}
}
impl From<retry::Error<Error>> for Error {
fn from (err: retry::Error<Error>) -> Error {
match err {
retry::Error::Operation { error, total_delay, tries } => {
error
}
}
}
}
pub trait Fighter {
fn power (&self) -> u8;
}

View File

@ -2,11 +2,17 @@ use lru_cache::LruCache;
use hyper::client::Client;
use select::document::Document;
use retry::retry;
use retry::delay::Fixed;
use std::io::Read;
use {Error, Fighter};
use parser::Parser;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;
pub struct Ninjas {
cache: LruCache<String, Ninja>,
client: Client
@ -29,7 +35,7 @@ impl Ninjas {
return Result::Ok(self.cache.get_mut(name).unwrap())
}
let ninja = self.get_by_name(name)?;
let ninja = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || self.get_by_name(name))?;
self.cache.insert(String::from(name), ninja);
Result::Ok(self.cache.get_mut(name).unwrap())
}

View File

@ -2,11 +2,17 @@ use lru_cache::LruCache;
use hyper::client::Client;
use select::document::Document;
use retry::retry;
use retry::delay::Fixed;
use std::io::Read;
use {Error, Fighter};
use parser::Parser;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;
pub struct Pirates {
cache: LruCache<String, Pirate>,
client: Client
@ -29,7 +35,7 @@ impl Pirates {
return Result::Ok(self.cache.get_mut(name).unwrap())
}
let pirate = self.get_by_name(name)?;
let pirate = retry(Fixed::from_millis(RETRY_WAIT_MILLIS).take(NUM_RETRIES), || self.get_by_name(name))?;
self.cache.insert(String::from(name), pirate);
Result::Ok(self.cache.get_mut(name).unwrap())
}