src/exporter/client: Trigger random node lookups on interval

This commit is contained in:
Max Inden 2020-04-12 14:53:54 +02:00
parent 6da53e4520
commit aa41fb8471
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
3 changed files with 17 additions and 6 deletions

1
Cargo.lock generated
View File

@ -1187,6 +1187,7 @@ dependencies = [
"env_logger", "env_logger",
"exit-future", "exit-future",
"futures 0.3.4", "futures 0.3.4",
"futures-timer 3.0.2",
"libp2p", "libp2p",
"libp2p-kad", "libp2p-kad",
"prometheus", "prometheus",

View File

@ -18,4 +18,5 @@ tide = "0.6"
exit-future = "0.2" exit-future = "0.2"
ctrlc = "3" ctrlc = "3"
structopt = "0.3" structopt = "0.3"
futures-timer = "3"

View File

@ -1,4 +1,5 @@
use futures::prelude::*; use futures::prelude::*;
use futures_timer::Delay;
use libp2p::{ use libp2p::{
core::{ core::{
self, either::EitherError, either::EitherOutput, multiaddr::Protocol, self, either::EitherError, either::EitherOutput, multiaddr::Protocol,
@ -7,10 +8,7 @@ use libp2p::{
dns, dns,
identify::{Identify, IdentifyEvent}, identify::{Identify, IdentifyEvent},
identity::Keypair, identity::Keypair,
kad::{ kad::{record::store::MemoryStore, Kademlia, KademliaConfig, KademliaEvent},
record::store::MemoryStore, Kademlia, KademliaConfig,
KademliaEvent,
},
mplex, noise, mplex, noise,
ping::{Ping, PingConfig, PingEvent}, ping::{Ping, PingConfig, PingEvent},
secio, secio,
@ -26,9 +24,13 @@ use std::{
usize, usize,
}; };
const RANDOM_WALK_INTERVAL: Duration = Duration::from_secs(10);
pub struct Client { pub struct Client {
swarm: Swarm<MyBehaviour>, swarm: Swarm<MyBehaviour>,
listening: bool, listening: bool,
random_walk: Delay,
} }
impl Client { impl Client {
@ -56,6 +58,8 @@ impl Client {
Ok(Client { Ok(Client {
swarm, swarm,
listening: false, listening: false,
random_walk: futures_timer::Delay::new(RANDOM_WALK_INTERVAL),
}) })
} }
} }
@ -63,8 +67,13 @@ impl Client {
// TODO: this should be a stream instead. // TODO: this should be a stream instead.
impl Stream for Client { impl Stream for Client {
type Item = Event; type Item = Event;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
match self.swarm.poll_next_unpin(cx) { if let Poll::Ready(()) = self.random_walk.poll_unpin(ctx) {
self.random_walk = Delay::new(RANDOM_WALK_INTERVAL);
self.swarm.kademlia.get_closest_peers(PeerId::random());
}
match self.swarm.poll_next_unpin(ctx) {
Poll::Ready(Some(event)) => return Poll::Ready(Some(event)), Poll::Ready(Some(event)) => return Poll::Ready(Some(event)),
Poll::Ready(None) => return Poll::Ready(None), Poll::Ready(None) => return Poll::Ready(None),
Poll::Pending => { Poll::Pending => {