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

Commit 59f373a

Browse files
authored
fix: providers leaking resources on dht construction (#194)
* fix: providers leaking resources on dht construction * chore: remove intervals validation on start and stop
1 parent 57a0411 commit 59f373a

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"delay": "^4.3.0",
7878
"dirty-chai": "^2.0.1",
7979
"it-pair": "^1.0.0",
80-
"libp2p": "libp2p/js-libp2p#0.28.x",
80+
"libp2p": "^0.28.0-rc.0",
8181
"lodash": "^4.17.11",
8282
"lodash.random": "^3.2.0",
8383
"lodash.range": "^3.2.0",

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class KadDHT extends EventEmitter {
185185
*/
186186
async start () {
187187
this._running = true
188+
this.providers.start()
188189
this._queryManager.start()
189190
await this.network.start()
190191

src/providers.js

+13-22
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,24 @@ class Providers {
5858
this.syncQueue = new Queue({ concurrency: 1 })
5959
}
6060

61+
/**
62+
* Start the provider cleanup service
63+
* @returns {void}
64+
*/
65+
start () {
66+
this._cleaner = setInterval(
67+
() => this._cleanup(),
68+
this.cleanupInterval
69+
)
70+
}
71+
6172
/**
6273
* Release any resources.
63-
*
6474
* @returns {void}
6575
*/
6676
stop () {
67-
if (this._cleaner) {
68-
clearInterval(this._cleaner)
69-
this._cleaner = null
70-
}
77+
clearInterval(this._cleaner)
78+
this._cleaner = null
7179
}
7280

7381
/**
@@ -155,23 +163,6 @@ class Providers {
155163
return provs
156164
}
157165

158-
get cleanupInterval () {
159-
return this._cleanupInterval
160-
}
161-
162-
set cleanupInterval (val) {
163-
this._cleanupInterval = val
164-
165-
if (this._cleaner) {
166-
clearInterval(this._cleaner)
167-
}
168-
169-
this._cleaner = setInterval(
170-
() => this._cleanup(),
171-
this.cleanupInterval
172-
)
173-
}
174-
175166
/**
176167
* Add a new provider for the given CID.
177168
*

test/providers.spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ describe('Providers', () => {
8888
providers.cleanupInterval = 100
8989
providers.provideValidity = 200
9090

91+
providers.start()
92+
9193
const cid = new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
9294
await Promise.all([
9395
providers.addProvider(cid, peerIds[0]),
@@ -104,6 +106,7 @@ describe('Providers', () => {
104106

105107
const provsAfter = await providers.getProviders(cid)
106108
expect(provsAfter).to.have.length(0)
109+
providers.stop()
107110
})
108111

109112
// slooow so only run when you need to

0 commit comments

Comments
 (0)