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

Commit ccae3f1

Browse files
author
Alan Shaw
committed
fix: discover peers faster
Previously we had to wait 10s (by default) **before** initial peer discovery, since the module uses an interval to re-emit discovered peers every 10s. This PR refactors `start` to do an initial discovery of the boostrap peers immediately after it has started (i.e. after the callback has been called). This addresses the problem where a call to `cat`/`get`/others immediately after the IPFS `ready` event would take at minimum 10s to get content stored on the preload nodes due to the initial delay in discovery. fixes #85 resolves ipfs/js-ipfs#1706 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent eb2d882 commit ccae3f1

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/index.js

+24-17
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const multiaddr = require('multiaddr')
66
const mafmt = require('mafmt')
77
const EventEmitter = require('events').EventEmitter
88
const debug = require('debug')
9-
const setImmediate = require('async/setImmediate')
9+
const nextTick = require('async/nextTick')
1010

11-
const log = debug('libp2p:railing')
12-
log.error = debug('libp2p:railing:error')
11+
const log = debug('libp2p:bootstrap')
12+
log.error = debug('libp2p:bootstrap:error')
1313

1414
function isIPFS (addr) {
1515
try {
@@ -28,29 +28,36 @@ class Bootstrap extends EventEmitter {
2828
}
2929

3030
start (callback) {
31-
setImmediate(() => callback())
31+
if (this._timer) {
32+
return nextTick(() => callback())
33+
}
3234

33-
if (this._timer) { return }
35+
this._timer = setInterval(() => this._discoverBootstrapPeers(), this._interval)
36+
37+
nextTick(() => {
38+
callback()
39+
this._discoverBootstrapPeers()
40+
})
41+
}
3442

35-
this._timer = setInterval(() => {
36-
this._list.forEach((candidate) => {
37-
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }
43+
_discoverBootstrapPeers () {
44+
this._list.forEach((candidate) => {
45+
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }
3846

39-
const ma = multiaddr(candidate)
47+
const ma = multiaddr(candidate)
4048

41-
const peerId = PeerId.createFromB58String(ma.getPeerId())
49+
const peerId = PeerId.createFromB58String(ma.getPeerId())
4250

43-
PeerInfo.create(peerId, (err, peerInfo) => {
44-
if (err) { return log.error('Invalid bootstrap peer id', err) }
45-
peerInfo.multiaddrs.add(ma)
46-
this.emit('peer', peerInfo)
47-
})
51+
PeerInfo.create(peerId, (err, peerInfo) => {
52+
if (err) { return log.error('Invalid bootstrap peer id', err) }
53+
peerInfo.multiaddrs.add(ma)
54+
this.emit('peer', peerInfo)
4855
})
49-
}, this._interval)
56+
})
5057
}
5158

5259
stop (callback) {
53-
setImmediate(callback)
60+
nextTick(callback)
5461

5562
if (this._timer) {
5663
clearInterval(this._timer)

test/bootstrap.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const Railing = require('../src')
4+
const Bootstrap = require('../src')
55
const peerList = require('./default-peers')
66
const partialValidPeerList = require('./some-invalid-peers')
77
const { expect } = require('chai')
@@ -10,7 +10,7 @@ const mafmt = require('mafmt')
1010
describe('bootstrap', () => {
1111
it('find the other peer', function (done) {
1212
this.timeout(5 * 1000)
13-
const r = new Railing({
13+
const r = new Bootstrap({
1414
list: peerList,
1515
interval: 2000
1616
})
@@ -22,7 +22,7 @@ describe('bootstrap', () => {
2222
it('not fail on malformed peers in peer list', function (done) {
2323
this.timeout(5 * 1000)
2424

25-
const r = new Railing({
25+
const r = new Bootstrap({
2626
list: partialValidPeerList,
2727
interval: 2000
2828
})

0 commit comments

Comments
 (0)