struct Nodes {
good_node_services: HashMap<u8, HashSet<SocketAddr>>,
nodes_to_state: HashMap<SocketAddr, Node>,
- state_next_scan: HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
+ state_next_scan: Vec<Vec<(Instant, SocketAddr)>>,
}
struct NodesMutRef<'a> {
good_node_services: &'a mut HashMap<u8, HashSet<SocketAddr>>,
nodes_to_state: &'a mut HashMap<SocketAddr, Node>,
- state_next_scan: &'a mut HashMap<AddressState, Vec<(Instant, SocketAddr)>>,
+ state_next_scan: &'a mut Vec<Vec<(Instant, SocketAddr)>>,
}
impl Nodes {
macro_rules! nodes_uninitd {
() => { {
- let mut state_vecs = HashMap::with_capacity(11);
- state_vecs.insert(AddressState::Untested, Vec::new());
- state_vecs.insert(AddressState::LowBlockCount, Vec::new());
- state_vecs.insert(AddressState::HighBlockCount, Vec::new());
- state_vecs.insert(AddressState::LowVersion, Vec::new());
- state_vecs.insert(AddressState::BadVersion, Vec::new());
- state_vecs.insert(AddressState::NotFullNode, Vec::new());
- state_vecs.insert(AddressState::ProtocolViolation, Vec::new());
- state_vecs.insert(AddressState::Timeout, Vec::new());
- state_vecs.insert(AddressState::TimeoutDuringRequest, Vec::new());
- state_vecs.insert(AddressState::Good, Vec::new());
- state_vecs.insert(AddressState::WasGood, Vec::new());
+ let mut state_vecs = Vec::with_capacity(AddressState::get_count() as usize);
+ for _ in 0..AddressState::get_count() {
+ state_vecs.push(Vec::new());
+ }
let mut good_node_services = HashMap::with_capacity(64);
for i in 0..64 {
good_node_services.insert(i, HashSet::new());
}
}
}
- res.state_next_scan.get_mut(&node.state).unwrap().push((Instant::now(), sockaddr));
+ res.state_next_scan[node.state.to_num() as usize].push((Instant::now(), sockaddr));
res.nodes_to_state.insert(sockaddr, node);
}
future::ok(res)
}
pub fn get_node_count(&self, state: AddressState) -> usize {
- self.nodes.read().unwrap().state_next_scan.get(&state).unwrap().len()
+ self.nodes.read().unwrap().state_next_scan[state.to_num() as usize].len()
}
pub fn get_regex(&self, _setting: RegexSetting) -> Arc<Regex> {
*self.subver_regex.write().unwrap() = Arc::new(value);
}
- pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) {
+ pub fn add_fresh_addrs<I: Iterator<Item=SocketAddr>>(&self, addresses: I) -> u64 {
+ let mut res = 0;
let mut nodes = self.nodes.write().unwrap();
let cur_time = Instant::now();
- for &(_, ref addr) in addresses {
- if let Ok(socketaddr) = addr.socket_addr() {
- match nodes.nodes_to_state.entry(socketaddr.clone()) {
- hash_map::Entry::Vacant(e) => {
- e.insert(Node {
- state: AddressState::Untested,
- last_services: 0,
- last_update: cur_time,
- last_good: Instant::now(),
- });
- nodes.state_next_scan.get_mut(&AddressState::Untested).unwrap().push((cur_time, socketaddr));
- },
- hash_map::Entry::Occupied(_) => {},
- }
- } else {
- //TODO: Handle onions
+ for addr in addresses {
+ match nodes.nodes_to_state.entry(addr.clone()) {
+ hash_map::Entry::Vacant(e) => {
+ e.insert(Node {
+ state: AddressState::Untested,
+ last_services: 0,
+ last_update: cur_time,
+ last_good: cur_time,
+ });
+ nodes.state_next_scan[AddressState::Untested.to_num() as usize].push((cur_time, addr));
+ res += 1;
+ },
+ hash_map::Entry::Occupied(_) => {},
}
}
+ res
+ }
+
+ pub fn add_fresh_nodes(&self, addresses: &Vec<(u32, Address)>) {
+ self.add_fresh_addrs(addresses.iter().filter_map(|(_, addr)| {
+ match addr.socket_addr() {
+ Ok(socketaddr) => Some(socketaddr),
+ Err(_) => None, // TODO: Handle onions
+ }
+ }));
}
pub fn set_node_state(&self, addr: SocketAddr, state: AddressState, services: u64) -> AddressState {
let mut nodes_lock = self.nodes.write().unwrap();
let nodes = nodes_lock.borrow_mut();
- let state_ref = nodes.nodes_to_state.get_mut(&addr).unwrap();
- let ret = state_ref.state;
let now = Instant::now();
+
+ let state_ref = nodes.nodes_to_state.entry(addr).or_insert(Node {
+ state: AddressState::Untested,
+ last_services: 0,
+ last_update: now,
+ last_good: now,
+ });
+ let ret = state_ref.state;
if (state_ref.state == AddressState::Good || state_ref.state == AddressState::WasGood)
&& state != AddressState::Good
&& state_ref.last_good >= now - Duration::from_secs(self.get_u64(U64Setting::WasGoodTimeout)) {
}
}
state_ref.last_services = 0;
- nodes.state_next_scan.get_mut(&AddressState::WasGood).unwrap().push((now, addr));
+ nodes.state_next_scan[AddressState::WasGood.to_num() as usize].push((now, addr));
} else {
state_ref.state = state;
if state == AddressState::Good {
state_ref.last_services = services;
state_ref.last_good = now;
}
- nodes.state_next_scan.get_mut(&state).unwrap().push((now, addr));
+ nodes.state_next_scan[state.to_num() as usize].push((now, addr));
}
state_ref.last_update = now;
ret
}
pub fn get_next_scan_nodes(&self) -> Vec<SocketAddr> {
- let mut res = Vec::with_capacity(600);
+ let results = 30 * self.get_u64(U64Setting::ConnsPerSec) as usize;
+ let per_bucket_results = results / (AddressState::get_count() as usize);
+ let mut res = Vec::with_capacity(results);
let cur_time = Instant::now();
+
let mut nodes = self.nodes.write().unwrap();
- for (state, state_nodes) in nodes.state_next_scan.iter_mut() {
- let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(*state)));
- let split_point = cmp::min(cmp::min(600 - res.len(), 60),
+ for (idx, state_nodes) in nodes.state_next_scan.iter_mut().enumerate() {
+ let cmp_time = cur_time - Duration::from_secs(self.get_u64(U64Setting::RescanInterval(AddressState::from_num(idx as u8).unwrap())));
+ let split_point = cmp::min(cmp::min(results - res.len(), results - (per_bucket_results * (AddressState::get_count() as usize - idx))),
state_nodes.binary_search_by(|a| a.0.cmp(&cmp_time)).unwrap_or_else(|idx| idx));
let mut new_nodes = state_nodes.split_off(split_point);
mem::swap(&mut new_nodes, state_nodes);