pub fn clean_peers(&self, max_count: usize) {
		let mut rm = vec![];

		// build a list of peers to be cleaned up
		for peer in self.peers.read().unwrap().values() {
			if peer.is_banned() {
				debug!(LOGGER, "clean_peers {:?}, peer banned", peer.info.addr);
				rm.push(peer.clone());
			} else if !peer.is_connected() {
				debug!(LOGGER, "clean_peers {:?}, not connected", peer.info.addr);
				rm.push(peer.clone());
			}
		}

		// now clean up peer map based on the list to remove
		{
			let mut peers = self.peers.write().unwrap();
			for p in rm {
				peers.remove(&p.info.addr);
			}
		}

		// ensure we do not have too many connected peers
		let excess_count = {
			let peer_count = self.peer_count() as usize;
			if peer_count > max_count {
				peer_count - max_count
			} else {
				0
			}
		};

		// map peers to addrs in a block to bound how long we keep the read lock for
		let addrs = self
			.connected_peers()
			.iter()
			.map(|x| x.info.addr.clone())
			.collect::<Vec<_>>();

		// now remove them taking a short-lived write lock each time
		// maybe better to take write lock once and remove them all?
		for x in addrs.iter().take(excess_count) {
			let mut peers = self.peers.write().unwrap();
			peers.remove(x);
		}
	}
/// Iterate over the peer list and prune all peers we have
/// lost connection to or have been deemed problematic.
/// Also avoid connected peer count getting too high.
Iterate over the peer list and prune all peers we have
lost connection to or have been deemed problematic.
Also avoid connected peer count getting too high.
Iterate over the peer list and prune all peers we have lost connection to or have been deemed problematic . Also avoid connected peer count getting too high .
pub fn clean_peers(&self, max_count: usize) {
		let mut rm = vec![];
		// build a list of peers to be cleaned up
		for peer in self.peers.read().unwrap().values() {
			if peer.is_banned() {
				debug!(LOGGER, "clean_peers {:?}, peer banned", peer.info.addr);
				rm.push(peer.clone());
			} else if !peer.is_connected() {
				debug!(LOGGER, "clean_peers {:?}, not connected", peer.info.addr);
				rm.push(peer.clone());
			}
		}
		// now clean up peer map based on the list to remove
		{
			let mut peers = self.peers.write().unwrap();
			for p in rm {
				peers.remove(&p.info.addr);
			}
		}
		// ensure we do not have too many connected peers
		let excess_count = {
			let peer_count = self.peer_count() as usize;
			if peer_count > max_count {
				peer_count - max_count
			} else {
				0
			}
		};
		// map peers to addrs in a block to bound how long we keep the read lock for
		let addrs = self
			.connected_peers()
			.iter()
			.map(|x| x.info.addr.clone())
			.collect::<Vec<_>>();
		// now remove them taking a short-lived write lock each time
		// maybe better to take write lock once and remove them all?
		for x in addrs.iter().take(excess_count) {
			let mut peers = self.peers.write().unwrap();
			peers.remove(x);
		}
	}
pub fn clean_peers ( & self , max_count : usize ) { let mut rm = vec ! [ ] ; // build a list of peers to be cleaned up for peer in self . peers . read ( ) . unwrap ( ) . values ( ) { if peer . is_banned ( ) { debug ! ( LOGGER " " peer info addr ) ; rm . push ( peer . clone ( ) ) ; } else if ! peer . is_connected ( ) { debug ! ( LOGGER " " peer info addr ) ; rm . push ( peer . clone ( ) ) ; } } // now clean up peer map based on the list to remove { let mut peers = self . peers . write ( ) . unwrap ( ) ; for p in rm { peers . remove ( & p . info . addr ) ; } } // ensure we do not have too many connected peers let excess_count = { let peer_count = self . peer_count ( ) as usize ; if peer_count > max_count { peer_count - max_count } else { 0 } } ; // map peers to addrs in a block to bound how long we keep the read lock for let addrs = self . connected_peers ( ) . iter ( ) . map ( | x | x . info . addr . clone ( ) ) . collect :: < Vec < _ > > ( ) ; // now remove them taking a short-lived write lock each time // maybe better to take write lock once and remove them all? for x in addrs . iter ( ) . take ( excess_count ) { let mut peers = self . peers . write ( ) . unwrap ( ) ; peers . remove ( x ) ; } }
Iterate over the peer list and prune all peers we have
lost connection to or have been deemed problematic.
Iterate over the peer list and prune all peers we have lost connection to or have been deemed problematic .
&self
max_count
usize