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

fix: discover peers faster #86

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')

const log = debug('libp2p:railing')
log.error = debug('libp2p:railing:error')
const log = debug('libp2p:bootstrap')
log.error = debug('libp2p:bootstrap:error')

function isIPFS (addr) {
try {
Expand All @@ -28,29 +28,36 @@ class Bootstrap extends EventEmitter {
}

start (callback) {
setImmediate(() => callback())
if (this._timer) {
return nextTick(() => callback())
}

if (this._timer) { return }
this._timer = setInterval(() => this._discoverBootstrapPeers(), this._interval)

nextTick(() => {
callback()
this._discoverBootstrapPeers()
})
}

this._timer = setInterval(() => {
this._list.forEach((candidate) => {
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }
_discoverBootstrapPeers () {
this._list.forEach((candidate) => {
if (!isIPFS(candidate)) { return log.error('Invalid multiaddr') }

const ma = multiaddr(candidate)
const ma = multiaddr(candidate)

const peerId = PeerId.createFromB58String(ma.getPeerId())
const peerId = PeerId.createFromB58String(ma.getPeerId())

PeerInfo.create(peerId, (err, peerInfo) => {
if (err) { return log.error('Invalid bootstrap peer id', err) }
peerInfo.multiaddrs.add(ma)
this.emit('peer', peerInfo)
})
PeerInfo.create(peerId, (err, peerInfo) => {
if (err) { return log.error('Invalid bootstrap peer id', err) }
peerInfo.multiaddrs.add(ma)
this.emit('peer', peerInfo)
})
}, this._interval)
})
}

stop (callback) {
setImmediate(callback)
nextTick(callback)

if (this._timer) {
clearInterval(this._timer)
Expand Down
6 changes: 3 additions & 3 deletions test/bootstrap.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env mocha */
'use strict'

const Railing = require('../src')
const Bootstrap = require('../src')
const peerList = require('./default-peers')
const partialValidPeerList = require('./some-invalid-peers')
const { expect } = require('chai')
Expand All @@ -10,7 +10,7 @@ const mafmt = require('mafmt')
describe('bootstrap', () => {
it('find the other peer', function (done) {
this.timeout(5 * 1000)
const r = new Railing({
const r = new Bootstrap({
list: peerList,
interval: 2000
})
Expand All @@ -22,7 +22,7 @@ describe('bootstrap', () => {
it('not fail on malformed peers in peer list', function (done) {
this.timeout(5 * 1000)

const r = new Railing({
const r = new Bootstrap({
list: partialValidPeerList,
interval: 2000
})
Expand Down