diff --git a/peerstore/src/json_peerstore.rs b/peerstore/src/json_peerstore.rs index 563ef489..85092f58 100644 --- a/peerstore/src/json_peerstore.rs +++ b/peerstore/src/json_peerstore.rs @@ -91,7 +91,10 @@ impl<'a> Peerstore for &'a JsonPeerstore { }); let list = query - .filter_map(|(key, _)| { + .filter_map(|(key, info)| { + if info.addrs().count() == 0 { + return None // all addresses are expired + } // We filter out invalid elements. This can happen if the JSON storage file was // corrupted or manually modified by the user. PeerId::from_bytes(bs58::decode(key).into_vec().ok()?).ok() diff --git a/peerstore/src/memory_peerstore.rs b/peerstore/src/memory_peerstore.rs index 0755abfc..90e3e005 100644 --- a/peerstore/src/memory_peerstore.rs +++ b/peerstore/src/memory_peerstore.rs @@ -75,7 +75,14 @@ impl<'a> Peerstore for &'a MemoryPeerstore { fn peers(self) -> Self::PeersIter { let lock = self.store.lock().unwrap(); - lock.keys().cloned().collect::>().into_iter() + lock.iter() + .filter_map(|(id, info)| { + if info.addrs().count() == 0 { + return None // all addresses are expired + } + Some(id.clone()) + }) + .collect::>().into_iter() } } diff --git a/peerstore/src/peer_info.rs b/peerstore/src/peer_info.rs index 28784bba..76d1c4c6 100644 --- a/peerstore/src/peer_info.rs +++ b/peerstore/src/peer_info.rs @@ -52,13 +52,16 @@ impl PeerInfo { /// /// > **Note**: Keep in mind that this function is racy because addresses can expire between /// > the moment when you get them and the moment when you process them. - // TODO: use -> impl Iterator eventually #[inline] - pub fn addrs<'a>(&'a self) -> Box + 'a> { + pub fn addrs<'a>(&'a self) -> impl Iterator + 'a { let now = SystemTime::now(); - Box::new(self.addrs.iter().filter_map( - move |&(ref addr, ref expires)| if *expires >= now { Some(addr) } else { None }, - )) + self.addrs.iter().filter_map(move |(addr, expires)| { + if *expires >= now { + Some(addr) + } else { + None + } + }) } /// Sets the list of addresses and their time-to-live.