Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
updater: apply exponential backoff after download failure
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva committed Mar 5, 2018
1 parent eeee90d commit 9654f29
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::fs;
use std::io::Write;
use std::path::{PathBuf};
use std::sync::{Arc, Weak};
use std::time::{Duration, Instant};

use ethcore::client::{BlockId, BlockChainClient, ChainNotify};
use ethsync::{SyncProvider};
Expand Down Expand Up @@ -82,6 +83,8 @@ struct UpdaterState {
capability: CapState,

disabled: bool,

backoff: Option<(u32, Instant)>,
}

/// Service for checking for updates and determining whether we can achieve consensus.
Expand Down Expand Up @@ -260,7 +263,19 @@ impl Updater {
let fetched = s.fetching.take().unwrap();
let dest = self.updates_path(&Self::update_file_name(&fetched.version));
if !dest.exists() {
let b = result.map_err(|e| (format!("Unable to fetch update ({}): {:?}", fetched.version, e), false))?;
let b = match result {
Ok(b) => {
s.backoff = None;
b
},
Err(e) => {
let mut n = s.backoff.map(|b| b.0 + 1).unwrap_or(1);
s.backoff = Some((n, Instant::now() + Duration::from_secs(2usize.pow(n) as u64)));

return Err((format!("Unable to fetch update ({}): {:?}", fetched.version, e), false));
},
};

info!(target: "updater", "Fetched latest version ({}) OK to {}", fetched.version, b.display());
fs::create_dir_all(dest.parent().expect("at least one thing pushed; qed")).map_err(|e| (format!("Unable to create updates path: {:?}", e), true))?;
fs::copy(&b, &dest).map_err(|e| (format!("Unable to copy update: {:?}", e), true))?;
Expand Down Expand Up @@ -320,12 +335,14 @@ impl Updater {
drop(s);
self.fetch_done(Ok(PathBuf::new()));
} else {
info!(target: "updater", "Attempting to get parity binary {}", b);
s.fetching = Some(latest.track.clone());
drop(s);
let weak_self = self.weak_self.lock().clone();
let f = move |r: Result<PathBuf, fetch::Error>| if let Some(this) = weak_self.upgrade() { this.fetch_done(r) };
self.fetcher.fetch(b, Box::new(f));
if s.backoff.iter().all(|&(_, instant)| Instant::now() >= instant) {
info!(target: "updater", "Attempting to get parity binary {}", b);
s.fetching = Some(latest.track.clone());
drop(s);
let weak_self = self.weak_self.lock().clone();
let f = move |r: Result<PathBuf, fetch::Error>| if let Some(this) = weak_self.upgrade() { this.fetch_done(r) };
self.fetcher.fetch(b, Box::new(f));
}
}
}
}
Expand Down

0 comments on commit 9654f29

Please sign in to comment.