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

Commit

Permalink
use constant durations (#8278)
Browse files Browse the repository at this point in the history
* use constant durations

* fix CI

* address comments
  • Loading branch information
rleungx authored and tomusdrw committed Apr 2, 2018
1 parent 68a08df commit 9c9ddac
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 84 deletions.
6 changes: 3 additions & 3 deletions dapps/node-health/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Reporting node's health.

use std::sync::Arc;
use std::time;
use std::time::Duration;
use futures::Future;
use futures::sync::oneshot;
use types::{HealthInfo, HealthStatus, Health};
Expand All @@ -26,7 +26,7 @@ use parity_reactor::Remote;
use parking_lot::Mutex;
use {SyncStatus};

const TIMEOUT_SECS: u64 = 5;
const TIMEOUT: Duration = Duration::from_secs(5);
const PROOF: &str = "Only one closure is invoked.";

/// A struct enabling you to query for node's health.
Expand Down Expand Up @@ -57,7 +57,7 @@ impl NodeHealth {
let _ = tx.lock().take().expect(PROOF).send(Ok(result));
Ok(())
}),
time::Duration::from_secs(TIMEOUT_SECS),
TIMEOUT,
move || {
let _ = tx2.lock().take().expect(PROOF).send(Err(()));
},
Expand Down
6 changes: 3 additions & 3 deletions dapps/src/handlers/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use handlers::{ContentHandler, StreamingHandler};
use page::local;
use {Embeddable};

const FETCH_TIMEOUT: u64 = 300;
const FETCH_TIMEOUT: Duration = Duration::from_secs(300);

pub enum ValidatorResponse {
Local(local::Dapp),
Expand All @@ -57,7 +57,7 @@ impl Default for FetchControl {
FetchControl {
abort: Arc::new(AtomicBool::new(false)),
listeners: Arc::new(Mutex::new(Vec::new())),
deadline: Instant::now() + Duration::from_secs(FETCH_TIMEOUT),
deadline: Instant::now() + FETCH_TIMEOUT,
}
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ impl Errors {
ContentHandler::error(
StatusCode::GatewayTimeout,
"Download Timeout",
&format!("Could not fetch content within {} seconds.", FETCH_TIMEOUT),
&format!("Could not fetch content within {} seconds.", FETCH_TIMEOUT.as_secs()),
None,
self.embeddable_on.clone(),
)
Expand Down
14 changes: 8 additions & 6 deletions ethcore/light/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const RECALCULATE_COSTS_TIMEOUT: TimerToken = 3;
const RECALCULATE_COSTS_INTERVAL_MS: u64 = 60 * 60 * 1000;

// minimum interval between updates.
const UPDATE_INTERVAL_MS: u64 = 5000;
const UPDATE_INTERVAL: Duration = Duration::from_millis(5000);

/// Supported protocol versions.
pub const PROTOCOL_VERSIONS: &'static [u8] = &[1];
Expand Down Expand Up @@ -109,8 +109,10 @@ mod packet {

// timeouts for different kinds of requests. all values are in milliseconds.
mod timeout {
pub const HANDSHAKE: u64 = 2500;
pub const ACKNOWLEDGE_UPDATE: u64 = 5000;
use std::time::Duration;

pub const HANDSHAKE: Duration = Duration::from_millis(2500);
pub const ACKNOWLEDGE_UPDATE: Duration = Duration::from_millis(5000);
pub const BASE: u64 = 1500; // base timeout for packet.

// timeouts per request within packet.
Expand Down Expand Up @@ -470,7 +472,7 @@ impl LightProtocol {
// the timer approach will skip 1 (possibly 2) in rare occasions.
if peer_info.sent_head == announcement.head_hash ||
peer_info.status.head_num >= announcement.head_num ||
now - peer_info.last_update < Duration::from_millis(UPDATE_INTERVAL_MS) {
now - peer_info.last_update < UPDATE_INTERVAL {
continue
}

Expand Down Expand Up @@ -606,7 +608,7 @@ impl LightProtocol {
let mut pending = self.pending_peers.write();
let slowpokes: Vec<_> = pending.iter()
.filter(|&(_, ref peer)| {
peer.last_update + Duration::from_millis(timeout::HANDSHAKE) <= now
peer.last_update + timeout::HANDSHAKE <= now
})
.map(|(&p, _)| p)
.collect();
Expand All @@ -619,7 +621,7 @@ impl LightProtocol {
}

// request and update ack timeouts
let ack_duration = Duration::from_millis(timeout::ACKNOWLEDGE_UPDATE);
let ack_duration = timeout::ACKNOWLEDGE_UPDATE;
{
for (peer_id, peer) in self.peers.read().iter() {
let peer = peer.lock();
Expand Down
4 changes: 2 additions & 2 deletions miner/src/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl ExternalMiner {
}
}

const ENTRY_TIMEOUT: u64 = 2;
const ENTRY_TIMEOUT: Duration = Duration::from_secs(2);

impl ExternalMinerService for ExternalMiner {
fn submit_hashrate(&self, hashrate: U256, id: H256) {
self.hashrates.lock().insert(id, (Instant::now() + Duration::from_secs(ENTRY_TIMEOUT), hashrate));
self.hashrates.lock().insert(id, (Instant::now() + ENTRY_TIMEOUT, hashrate));
}

fn hashrate(&self) -> U256 {
Expand Down
4 changes: 2 additions & 2 deletions parity/light_helpers/queue_cull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const TOKEN: TimerToken = 1;
const TIMEOUT_MS: u64 = 1000 * 60 * 10;

// But make each attempt last only 9 minutes
const PURGE_TIMEOUT_MS: u64 = 1000 * 60 * 9;
const PURGE_TIMEOUT: Duration = Duration::from_millis(1000 * 60 * 9);

/// Periodically culls the transaction queue of mined transactions.
pub struct QueueCull<T> {
Expand Down Expand Up @@ -100,6 +100,6 @@ impl<T: LightChainClient + 'static> IoHandler<ClientIoMessage> for QueueCull<T>
future::Either::B(future::ok(()))
},
}
}, Duration::from_millis(PURGE_TIMEOUT_MS), || {})
}, PURGE_TIMEOUT, || {})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::collections::{BTreeSet, BTreeMap, VecDeque};
use std::fmt::{Debug, Formatter, Error as FmtError};
use std::time;
use std::time::Duration;
use std::sync::Arc;
use parking_lot::{Condvar, Mutex};
use ethereum_types::Address;
Expand Down Expand Up @@ -252,7 +252,7 @@ impl SessionImpl {
}

/// Wait for session completion.
pub fn wait(&self, timeout: Option<time::Duration>) -> Result<Public, Error> {
pub fn wait(&self, timeout: Option<Duration>) -> Result<Public, Error> {
Self::wait_session(&self.completed, &self.data, timeout, |data| data.joint_public_and_secret.clone()
.map(|r| r.map(|r| r.0.clone())))
}
Expand Down Expand Up @@ -932,9 +932,9 @@ pub fn check_threshold(threshold: usize, nodes: &BTreeSet<NodeId>) -> Result<(),

#[cfg(test)]
pub mod tests {
use std::time;
use std::sync::Arc;
use std::collections::{BTreeSet, BTreeMap, VecDeque};
use std::time::Duration;
use tokio_core::reactor::Core;
use ethereum_types::Address;
use ethkey::{Random, Generator, Public, KeyPair};
Expand Down Expand Up @@ -1386,12 +1386,12 @@ pub mod tests {
run_clusters(&clusters);

// establish connections
loop_until(&mut core, time::Duration::from_millis(300), || clusters.iter().all(all_connections_established));
loop_until(&mut core, Duration::from_millis(300), || clusters.iter().all(all_connections_established));

// run session to completion
let session_id = SessionId::default();
let session = clusters[0].client().new_generation_session(session_id, Public::default(), threshold).unwrap();
loop_until(&mut core, time::Duration::from_millis(1000), || session.joint_public_and_secret().is_some());
loop_until(&mut core, Duration::from_millis(1000), || session.joint_public_and_secret().is_some());
}
}

Expand Down
Loading

0 comments on commit 9c9ddac

Please sign in to comment.