fix: invoke peer exchange query immediately (#1115)

when the query is set in an interval, it first
gets invoked when the interval time is reached.
refactor: invoke it immmediately, and then set an
interval
This commit is contained in:
Danish Arora 2023-01-11 11:11:49 +05:30 committed by GitHub
parent 0b083201c6
commit fcd500e045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 54 deletions

View File

@ -55,8 +55,62 @@ export class PeerExchangeDiscovery
if (!protocols.includes(PeerExchangeCodec) || this.intervals.get(peerId))
return;
await this.query(peerId);
const interval = setInterval(async () => {
await this.peerExchange.query(
await this.query(peerId);
}, PEER_EXCHANGE_QUERY_INTERVAL);
this.intervals.set(peerId, interval);
};
constructor(components: PeerExchangeComponents, options: Options = {}) {
super();
this.components = components;
this.peerExchange = new WakuPeerExchange(components);
this.options = options;
this.isStarted = false;
}
/**
* Start emitting events
*/
start(): void {
if (this.isStarted) {
return;
}
log("Starting peer exchange node discovery, discovering peers");
this.components.peerStore.addEventListener(
"change:protocols",
this.eventHandler
);
}
/**
* Remove event listener
*/
stop(): void {
if (!this.isStarted) return;
log("Stopping peer exchange node discovery");
this.isStarted = false;
this.intervals.forEach((interval) => clearInterval(interval));
this.components.peerStore.removeEventListener(
"change:protocols",
this.eventHandler
);
}
get [symbol](): true {
return true;
}
get [Symbol.toStringTag](): string {
return "@waku/peer-exchange";
}
private query(peerId: PeerId): Promise<void> {
return this.peerExchange.query(
{
numPeers: DEFAULT_PEER_EXCHANGE_REQUEST_NODES,
peerId,
@ -110,55 +164,6 @@ export class PeerExchangeDiscovery
}
}
);
}, PEER_EXCHANGE_QUERY_INTERVAL);
this.intervals.set(peerId, interval);
};
constructor(components: PeerExchangeComponents, options: Options = {}) {
super();
this.components = components;
this.peerExchange = new WakuPeerExchange(components);
this.options = options;
this.isStarted = false;
}
/**
* Start emitting events
*/
start(): void {
if (this.isStarted) {
return;
}
log("Starting peer exchange node discovery, discovering peers");
this.components.peerStore.addEventListener(
"change:protocols",
this.eventHandler
);
}
/**
* Remove event listener
*/
stop(): void {
if (!this.isStarted) return;
log("Stopping peer exchange node discovery");
this.isStarted = false;
this.intervals.forEach((interval) => clearInterval(interval));
this.components.peerStore.removeEventListener(
"change:protocols",
this.eventHandler
);
}
get [symbol](): true {
return true;
}
get [Symbol.toStringTag](): string {
return "@waku/peer-exchange";
}
}