Remove the time dependency where possible (#8100)
This commit is contained in:
committed by
Marek Kotewicz
parent
5c47116889
commit
113c35af0a
@@ -19,11 +19,11 @@ use std::net::SocketAddr;
|
||||
use std::collections::{HashSet, HashMap, BTreeMap, VecDeque};
|
||||
use std::mem;
|
||||
use std::default::Default;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
use mio::*;
|
||||
use mio::deprecated::{Handler, EventLoop};
|
||||
use mio::udp::*;
|
||||
use hash::keccak;
|
||||
use time;
|
||||
use ethereum_types::{H256, H520};
|
||||
use rlp::*;
|
||||
use node_table::*;
|
||||
@@ -59,7 +59,7 @@ pub struct NodeEntry {
|
||||
pub struct BucketEntry {
|
||||
pub address: NodeEntry,
|
||||
pub id_hash: H256,
|
||||
pub timeout: Option<u64>,
|
||||
pub timeout: Option<Instant>,
|
||||
}
|
||||
|
||||
pub struct NodeBucket {
|
||||
@@ -170,7 +170,7 @@ impl Discovery {
|
||||
if bucket.nodes.len() > BUCKET_SIZE {
|
||||
//ping least active node
|
||||
let last = bucket.nodes.back_mut().expect("Last item is always present when len() > 0");
|
||||
last.timeout = Some(time::precise_time_ns());
|
||||
last.timeout = Some(Instant::now());
|
||||
Some(last.address.endpoint.clone())
|
||||
} else { None }
|
||||
};
|
||||
@@ -262,7 +262,7 @@ impl Discovery {
|
||||
for i in 0 .. source.item_count() {
|
||||
rlp.append_raw(source.at(i).as_raw(), 1);
|
||||
}
|
||||
let timestamp = time::get_time().sec as u32 + 60;
|
||||
let timestamp = 60 + SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs() as u32;
|
||||
rlp.append(×tamp);
|
||||
|
||||
let bytes = rlp.drain();
|
||||
@@ -394,7 +394,8 @@ impl Discovery {
|
||||
|
||||
/// Validate that given timestamp is in within one second of now or in the future
|
||||
fn check_timestamp(&self, timestamp: u64) -> Result<(), Error> {
|
||||
if self.check_timestamps && timestamp < time::get_time().sec as u64{
|
||||
let secs_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
|
||||
if self.check_timestamps && timestamp < secs_since_epoch {
|
||||
debug!(target: "discovery", "Expired packet");
|
||||
return Err(ErrorKind::Expired.into());
|
||||
}
|
||||
@@ -504,12 +505,12 @@ impl Discovery {
|
||||
}
|
||||
|
||||
fn check_expired(&mut self, force: bool) -> HashSet<NodeId> {
|
||||
let now = time::precise_time_ns();
|
||||
let now = Instant::now();
|
||||
let mut removed: HashSet<NodeId> = HashSet::new();
|
||||
for bucket in &mut self.node_buckets {
|
||||
bucket.nodes.retain(|node| {
|
||||
if let Some(timeout) = node.timeout {
|
||||
if !force && now - timeout < PING_TIMEOUT_MS * 1000_0000 {
|
||||
if !force && now.duration_since(timeout) < Duration::from_millis(PING_TIMEOUT_MS) {
|
||||
true
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -67,7 +67,6 @@ extern crate mio;
|
||||
extern crate tiny_keccak;
|
||||
extern crate crypto as rcrypto;
|
||||
extern crate rand;
|
||||
extern crate time;
|
||||
extern crate ansi_term; //TODO: remove this
|
||||
extern crate rustc_hex;
|
||||
extern crate igd;
|
||||
|
||||
@@ -18,6 +18,7 @@ use std::{str, io};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::*;
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use mio::*;
|
||||
use mio::deprecated::{Handler, EventLoop};
|
||||
@@ -32,7 +33,6 @@ use network::{SessionCapabilityInfo, HostInfo as HostInfoTrait};
|
||||
use host::*;
|
||||
use node_table::NodeId;
|
||||
use stats::NetworkStats;
|
||||
use time;
|
||||
use snappy;
|
||||
|
||||
// Timeout must be less than (interval - 1).
|
||||
@@ -59,8 +59,8 @@ pub struct Session {
|
||||
had_hello: bool,
|
||||
/// Session is no longer active flag.
|
||||
expired: bool,
|
||||
ping_time_ns: u64,
|
||||
pong_time_ns: Option<u64>,
|
||||
ping_time: Instant,
|
||||
pong_time: Option<Instant>,
|
||||
state: State,
|
||||
// Protocol states -- accumulates pending packets until signaled as ready.
|
||||
protocol_states: HashMap<ProtocolId, ProtocolState>,
|
||||
@@ -123,8 +123,8 @@ impl Session {
|
||||
remote_address: "Handshake".to_owned(),
|
||||
local_address: local_addr,
|
||||
},
|
||||
ping_time_ns: 0,
|
||||
pong_time_ns: None,
|
||||
ping_time: Instant::now(),
|
||||
pong_time: None,
|
||||
expired: false,
|
||||
protocol_states: HashMap::new(),
|
||||
compression: false,
|
||||
@@ -299,13 +299,13 @@ impl Session {
|
||||
if let State::Handshake(_) = self.state {
|
||||
return true;
|
||||
}
|
||||
let timed_out = if let Some(pong) = self.pong_time_ns {
|
||||
pong - self.ping_time_ns > PING_TIMEOUT_SEC * 1000_000_000
|
||||
let timed_out = if let Some(pong) = self.pong_time {
|
||||
pong.duration_since(self.ping_time) > Duration::from_secs(PING_TIMEOUT_SEC)
|
||||
} else {
|
||||
time::precise_time_ns() - self.ping_time_ns > PING_TIMEOUT_SEC * 1000_000_000
|
||||
self.ping_time.elapsed() > Duration::from_secs(PING_TIMEOUT_SEC)
|
||||
};
|
||||
|
||||
if !timed_out && time::precise_time_ns() - self.ping_time_ns > PING_INTERVAL_SEC * 1000_000_000 {
|
||||
if !timed_out && self.ping_time.elapsed() > Duration::from_secs(PING_INTERVAL_SEC) {
|
||||
if let Err(e) = self.send_ping(io) {
|
||||
debug!("Error sending ping message: {:?}", e);
|
||||
}
|
||||
@@ -368,9 +368,11 @@ impl Session {
|
||||
Ok(SessionData::Continue)
|
||||
},
|
||||
PACKET_PONG => {
|
||||
let time = time::precise_time_ns();
|
||||
self.pong_time_ns = Some(time);
|
||||
self.info.ping_ms = Some((time - self.ping_time_ns) / 1000_000);
|
||||
let time = Instant::now();
|
||||
self.pong_time = Some(time);
|
||||
let ping_elapsed = time.duration_since(self.ping_time);
|
||||
self.info.ping_ms = Some(ping_elapsed.as_secs() * 1_000 +
|
||||
ping_elapsed.subsec_nanos() as u64 / 1_000_000);
|
||||
Ok(SessionData::Continue)
|
||||
},
|
||||
PACKET_GET_PEERS => Ok(SessionData::None), //TODO;
|
||||
@@ -482,11 +484,11 @@ impl Session {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Senf ping packet
|
||||
/// Send ping packet
|
||||
pub fn send_ping<Message>(&mut self, io: &IoContext<Message>) -> Result<(), Error> where Message: Send + Sync + Clone {
|
||||
self.send_packet(io, None, PACKET_PING, &EMPTY_LIST_RLP)?;
|
||||
self.ping_time_ns = time::precise_time_ns();
|
||||
self.pong_time_ns = None;
|
||||
self.ping_time = Instant::now();
|
||||
self.pong_time = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user