Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
randomWalk - revert to using timeoutId to avoid potential bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Jun 7, 2019
1 parent a53f875 commit fdea996
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 12 additions & 8 deletions src/random-walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RandomWalk {
this._options = { ...c.defaultRandomWalk, ...options }
this._kadDHT = dht
this.log = logger(dht.peerInfo.id, 'random-walk')
this._running = false
this._timeoutId = undefined
}

/**
Expand All @@ -40,12 +40,10 @@ class RandomWalk {
*/
start () {
// Don't run twice
if (this._running || !this._options.enabled) { return }

this._running = true
if (this._timeoutId || !this._options.enabled) { return }

// Start doing random walks after `this._options.delay`
setTimeout(() => {
this._timeoutId = setTimeout(() => {
// Start runner immediately
this._runPeriodically()
}, this._options.delay)
Expand All @@ -58,7 +56,10 @@ class RandomWalk {
* @returns {void}
*/
stop () {
this._running = false
if (this._timeoutId) {
clearTimeout(this._timeoutId)
this._timeoutId = undefined
}
this._controller && this._controller.abort()
}

Expand All @@ -69,14 +70,17 @@ class RandomWalk {
*/
async _runPeriodically () {
while (true) {
if (!this._running) return
// exit if the walk has been stopped
if (!this._timeoutId) return
try {
await this._walk(this._options.queriesPerPeriod, this._options.timeout)
} catch (err) {
this._kadDHT._log.error('random-walk:error', err)
}
// Each subsequent walk should run on a `this._options.interval` interval
await new Promise(resolve => setTimeout(resolve, this._options.interval))
await new Promise(resolve => {
this._timeoutId = setTimeout(resolve, this._options.interval)
})
}
}

Expand Down
11 changes: 7 additions & 4 deletions test/random-walk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ describe('Random Walk', () => {
findPeerStub.onCall(2).callsArgWith(2, error)
findPeerStub.callsArgWith(2, { code: 'ERR_NOT_FOUND' })

let err
try {
await randomWalk._walk(queries, 1e3)
} finally {
expect(findPeerStub.callCount).to.eql(5)
} catch (_err) {
err = _err
}
expect(err.message).to.include('ERR_BOOM')
expect(findPeerStub.callCount).to.eql(5)
})

it('should ignore timeout errors and keep walking', async () => {
Expand Down Expand Up @@ -222,9 +225,9 @@ describe('Random Walk', () => {
interval: 100e3
})
randomWalk.start()
expect(randomWalk._running).to.eql(true)
expect(randomWalk._timeoutId).to.exist()
randomWalk.stop()
expect(randomWalk._running).to.eql(false)
expect(randomWalk._timeoutId).to.eql(undefined)
})

it('should cancel the walk if already running', (done) => {
Expand Down

0 comments on commit fdea996

Please sign in to comment.