@@ -8,45 +8,56 @@ const multihashing = require('multihashing-async')
88const PeerId = require ( 'peer-id' )
99const assert = require ( 'assert' )
1010const c = require ( './constants' )
11+ const { logger } = require ( './utils' )
1112
1213const errcode = require ( 'err-code' )
1314
1415class RandomWalk {
15- constructor ( kadDHT ) {
16- assert ( kadDHT , 'Random Walk needs an instance of the Kademlia DHT' )
16+ /**
17+ * @constructor
18+ * @param {DHT } dht
19+ * @param {object } options
20+ * @param {randomWalkOptions.enabled } options.enabled
21+ * @param {randomWalkOptions.queriesPerPeriod } options.queriesPerPeriod
22+ * @param {randomWalkOptions.interval } options.interval
23+ * @param {randomWalkOptions.timeout } options.timeout
24+ * @param {randomWalkOptions.delay } options.delay
25+ * @param {DHT } options.dht
26+ */
27+ constructor ( dht , options ) {
28+ this . _options = { ...c . defaultRandomWalk , ...options }
29+ assert ( dht , 'Random Walk needs an instance of the Kademlia DHT' )
1730 this . _runningHandle = null
18- this . _kadDHT = kadDHT
31+ this . _kadDHT = dht
32+ this . log = logger ( dht . peerInfo . id , 'random-walk' )
1933 }
2034
2135 /**
2236 * Start the Random Walk process. This means running a number of queries
2337 * every interval requesting random data. This is done to keep the dht
2438 * healthy over time.
2539 *
26- * @param {number } [queries=1] - how many queries to run per period
27- * @param {number } [period=300000] - how often to run the the random-walk process, in milliseconds (5min)
28- * @param {number } [timeout=10000] - how long to wait for the the random-walk query to run, in milliseconds (10s)
2940 * @returns {void }
3041 */
31- start ( queries = c . defaultRandomWalk . queriesPerPeriod , period = c . defaultRandomWalk . interval , timeout = c . defaultRandomWalk . timeout ) {
42+ start ( ) {
3243 // Don't run twice
33- if ( this . _running ) { return }
44+ if ( this . _running || ! this . _options . enabled ) { return }
3445
3546 // Create running handle
3647 const runningHandle = {
3748 _onCancel : null ,
3849 _timeoutId : null ,
39- runPeriodically : ( fn , period ) => {
50+ runPeriodically : ( walk , period ) => {
4051 runningHandle . _timeoutId = setTimeout ( ( ) => {
4152 runningHandle . _timeoutId = null
4253
43- fn ( ( nextPeriod ) => {
54+ walk ( ( nextPeriod ) => {
4455 // Was walk cancelled while fn was being called?
4556 if ( runningHandle . _onCancel ) {
4657 return runningHandle . _onCancel ( )
4758 }
4859 // Schedule next
49- runningHandle . runPeriodically ( fn , nextPeriod )
60+ runningHandle . runPeriodically ( walk , nextPeriod )
5061 } )
5162 } , period )
5263 } ,
@@ -61,10 +72,15 @@ class RandomWalk {
6172 }
6273 }
6374
64- // Start runner
65- runningHandle . runPeriodically ( ( done ) => {
66- this . _walk ( queries , timeout , ( ) => done ( period ) )
67- } , period )
75+ // Start doing random walks after `this._options.delay`
76+ runningHandle . _timeoutId = setTimeout ( ( ) => {
77+ // Start runner immediately
78+ runningHandle . runPeriodically ( ( done ) => {
79+ // Each subsequent walk should run on a `this._options.interval` interval
80+ this . _walk ( this . _options . queriesPerPeriod , this . _options . timeout , ( ) => done ( this . _options . interval ) )
81+ } , 0 )
82+ } , this . _options . delay )
83+
6884 this . _runningHandle = runningHandle
6985 }
7086
@@ -96,7 +112,7 @@ class RandomWalk {
96112 * @private
97113 */
98114 _walk ( queries , walkTimeout , callback ) {
99- this . _kadDHT . _log ( 'random-walk: start')
115+ this . log ( ' start')
100116
101117 times ( queries , ( i , cb ) => {
102118 waterfall ( [
@@ -106,11 +122,11 @@ class RandomWalk {
106122 } , walkTimeout ) ( cb )
107123 ] , ( err ) => {
108124 if ( err ) {
109- this . _kadDHT . _log . error ( 'random-walk: error' , err )
125+ this . log . error ( 'query finished with error' , err )
110126 return callback ( err )
111127 }
112128
113- this . _kadDHT . _log ( 'random-walk: done')
129+ this . log ( ' done')
114130 callback ( null )
115131 } )
116132 } )
@@ -126,7 +142,7 @@ class RandomWalk {
126142 * @private
127143 */
128144 _query ( id , callback ) {
129- this . _kadDHT . _log ( 'random-walk: query:%s', id . toB58String ( ) )
145+ this . log ( ' query:%s', id . toB58String ( ) )
130146
131147 this . _kadDHT . findPeer ( id , ( err , peer ) => {
132148 if ( err . code === 'ERR_NOT_FOUND' ) {
@@ -136,7 +152,7 @@ class RandomWalk {
136152 if ( err ) {
137153 return callback ( err )
138154 }
139- this . _kadDHT . _log ( 'random-walk: query:found', err , peer )
155+ this . log ( ' query:found', peer )
140156
141157 // wait what, there was something found? Lucky day!
142158 callback ( errcode ( new Error ( `random-walk: ACTUALLY FOUND PEER: ${ peer } , ${ id . toB58String ( ) } ` ) , 'ERR_FOUND_RANDOM_PEER' ) )
0 commit comments