Skip to content

Commit

Permalink
Make searchInterval optional, handle infinity case
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Jul 27, 2021
1 parent da78f53 commit 0797960
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/libp2p/discv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { createNodeId, ENR } from "../enr";
import { IDiscv5Config } from "../config";
import { toBuffer } from "../util";

// Default to 0ms between automatic searches
// 0ms is 'backwards compatible' with the prior behavior (always be searching)
// Furthere analysis should be done to determine a good number
const DEFAULT_SEARCH_INTERVAL_MS = 0;

export interface IDiscv5DiscoveryInputOptions extends Partial<IDiscv5Config> {
/**
* Local ENR associated with the local libp2p peer id
Expand All @@ -26,8 +31,12 @@ export interface IDiscv5DiscoveryInputOptions extends Partial<IDiscv5Config> {
bootEnrs: ENRInput[];
/**
* Amount of time in milliseconds to wait between lookups
*
* Set to Infinity to disable automatic lookups entirely
*
* Default value is 0 (no wait)
*/
searchInterval: number;
searchInterval?: number;
/**
* Optional metrics
*/
Expand Down Expand Up @@ -63,7 +72,7 @@ export class Discv5Discovery extends EventEmitter {
config: options,
metrics: options.metrics,
});
this.searchInterval = options.searchInterval;
this.searchInterval = options.searchInterval ?? DEFAULT_SEARCH_INTERVAL_MS;
this.started = false;
this.controller = new AbortController();
options.bootEnrs.forEach((bootEnr) => this.discv5.addEnr(bootEnr));
Expand Down Expand Up @@ -91,6 +100,7 @@ export class Discv5Discovery extends EventEmitter {
}

async findPeers(): Promise<void> {
if (this.searchInterval === Infinity) return;
while (this.started) {
// Search for random nodes
// emit discovered on all finds
Expand All @@ -102,6 +112,7 @@ export class Discv5Discovery extends EventEmitter {
await this.handleEnr(enr);
}
try {
if (this.searchInterval === Infinity) return;
await sleep(this.searchInterval, this.controller.signal);
} catch (e) {
return;
Expand Down

0 comments on commit 0797960

Please sign in to comment.