Skip to content

Commit 84db9ce

Browse files
committed
feat: make start and stop be async, fix a bunch of bugs in between, detect a couple of others
1 parent 108432e commit 84db9ce

File tree

11 files changed

+251
-220
lines changed

11 files changed

+251
-220
lines changed

src/decision-engine/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const debug = require('debug')
44
const each = require('async/each')
55
const eachSeries = require('async/eachSeries')
66
const waterfall = require('async/waterfall')
7+
const setImmediate = require('async/setImmediate')
8+
79
const map = require('async/map')
810
const debounce = require('lodash.debounce')
911
const uniqWith = require('lodash.uniqwith')
@@ -271,12 +273,14 @@ class DecisionEngine {
271273
return l
272274
}
273275

274-
start () {
276+
start (callback) {
275277
this._running = true
278+
setImmediate(() => callback())
276279
}
277280

278-
stop () {
281+
stop (callback) {
279282
this._running = false
283+
setImmediate(() => callback())
280284
}
281285
}
282286

src/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,21 +386,25 @@ class Bitswap {
386386
*
387387
* @returns {void}
388388
*/
389-
start () {
390-
this.wm.run()
391-
this.network.start()
392-
this.engine.start()
389+
start (callback) {
390+
series([
391+
(cb) => this.wm.start(cb),
392+
(cb) => this.network.start(cb),
393+
(cb) => this.engine.start(cb)
394+
], callback)
393395
}
394396

395397
/**
396-
* Stoop the bitswap node.
398+
* Stop the bitswap node.
397399
*
398400
* @returns {void}
399401
*/
400-
stop () {
401-
this.wm.stop()
402-
this.network.stop()
403-
this.engine.stop()
402+
stop (callback) {
403+
series([
404+
(cb) => this.wm.stop(cb),
405+
(cb) => this.network.stop(cb),
406+
(cb) => this.engine.stop(cb)
407+
], callback)
404408
}
405409
}
406410

src/network.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const lp = require('pull-length-prefixed')
44
const pull = require('pull-stream')
55
const waterfall = require('async/waterfall')
66
const each = require('async/each')
7+
const setImmediate = require('async/setImmediate')
78

89
const Message = require('./types/message')
910
const CONSTANTS = require('./constants')
@@ -27,7 +28,7 @@ class Network {
2728
// this.libp2p.swarm.setMaxListeners(CONSTANTS.maxListeners)
2829
}
2930

30-
start () {
31+
start (callback) {
3132
this._running = true
3233
// bind event listeners
3334
this._onPeerConnect = this._onPeerConnect.bind(this)
@@ -41,18 +42,24 @@ class Network {
4142
this.libp2p.on('peer:disconnect', this._onPeerDisconnect)
4243

4344
// All existing connections are like new ones for us
44-
this.libp2p.peerBook.getAllArray().filter((peer) => peer.isConnected())
45-
.forEach((peer) => this._onPeerConnect((peer)))
45+
this.libp2p.peerBook
46+
.getAllArray()
47+
.filter((peer) => peer.isConnected())
48+
.forEach((peer) => this._onPeerConnect((peer)))
49+
50+
setImmediate(() => callback())
4651
}
4752

48-
stop () {
53+
stop (callback) {
4954
this._running = false
5055

5156
this.libp2p.unhandle(BITSWAP100)
5257
if (!this.b100Only) { this.libp2p.unhandle(BITSWAP110) }
5358

5459
this.libp2p.removeListener('peer:connect', this._onPeerConnect)
5560
this.libp2p.removeListener('peer:disconnect', this._onPeerDisconnect)
61+
62+
setImmediate(() => callback())
5663
}
5764

5865
// Handles both types of bitswap messgages
@@ -66,18 +73,15 @@ class Network {
6673
pull.asyncMap((data, cb) => Message.deserialize(data, cb)),
6774
pull.asyncMap((msg, cb) => {
6875
conn.getPeerInfo((err, peerInfo) => {
69-
if (err) {
70-
return cb(err)
71-
}
76+
if (err) { return cb(err) }
77+
7278
// log('data from', peerInfo.id.toB58String())
7379
this.bitswap._receiveMessage(peerInfo.id, msg, cb)
7480
})
7581
}),
7682
pull.onEnd((err) => {
7783
log('ending connection')
78-
if (err) {
79-
return this.bitswap._receiveError(err)
80-
}
84+
if (err) { return this.bitswap._receiveError(err) }
8185
})
8286
)
8387
}
@@ -147,27 +151,21 @@ class Network {
147151

148152
// Dial to the peer and try to use the most recent Bitswap
149153
_dialPeer (peer, callback) {
150-
// dialByPeerInfo throws if no network is there
151-
try {
152-
// Attempt Bitswap 1.1.0
153-
this.libp2p.dial(peer, BITSWAP110, (err, conn) => {
154-
if (err) {
155-
// Attempt Bitswap 1.0.0
156-
this.libp2p.dial(peer, BITSWAP100, (err, conn) => {
157-
if (err) {
158-
return callback(err)
159-
}
160-
161-
callback(null, conn, BITSWAP100)
162-
})
163-
return
164-
}
165-
166-
callback(null, conn, BITSWAP110)
167-
})
168-
} catch (err) {
169-
return callback(err)
170-
}
154+
// Attempt Bitswap 1.1.0
155+
this.libp2p.dial(peer, BITSWAP110, (err, conn) => {
156+
if (err) {
157+
// Attempt Bitswap 1.0.0
158+
this.libp2p.dial(peer, BITSWAP100, (err, conn) => {
159+
if (err) { return callback(err) }
160+
161+
callback(null, conn, BITSWAP100)
162+
})
163+
164+
return
165+
}
166+
167+
callback(null, conn, BITSWAP110)
168+
})
171169
}
172170
}
173171

src/types/wantlist/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class Wantlist {
4848
}
4949
}
5050

51+
forEach (fn) {
52+
return this.set.forEach(fn)
53+
}
54+
5155
entries () {
5256
return this.set.entries()
5357
}

src/want-manager/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Message = require('../types/message')
66
const Wantlist = require('../types/wantlist')
77
const CONSTANTS = require('../constants')
88
const MsgQueue = require('./msg-queue')
9+
const setImmediate = require('async/setImmediate')
910

1011
const log = debug('bitswap:wantmanager')
1112
log.error = debug('bitswap:wantmanager:error')
@@ -111,24 +112,24 @@ module.exports = class WantManager {
111112
this._stopPeerHandler(peerId)
112113
}
113114

114-
run () {
115+
start (callback) {
116+
// resend entire wantlist every so often
115117
this.timer = setInterval(() => {
116-
// resend entirew wantlist every so often
117118
const fullwantlist = new Message(true)
118-
for (let entry of this.wantlist.entries()) {
119+
this.wantlist.forEach((entry) => {
119120
fullwantlist.addEntry(entry[1].cid, entry[1].priority)
120-
}
121-
122-
this.peers.forEach((p) => {
123-
p.addMessage(fullwantlist)
124121
})
122+
123+
this.peers.forEach((p) => p.addMessage(fullwantlist))
125124
}, 10 * 1000)
125+
126+
setImmediate(() => callback())
126127
}
127128

128-
stop () {
129-
for (let mq of this.peers.values()) {
130-
this.disconnected(mq.peerId)
131-
}
129+
stop (callback) {
130+
this.peers.forEach((mq) => this.disconnected(mq.peerId))
131+
132132
clearInterval(this.timer)
133+
setImmediate(() => callback())
133134
}
134135
}

0 commit comments

Comments
 (0)