Browse Source

add .bootstrap and fix ping bug

v4
Mathias Buus 8 years ago
parent
commit
fbfbca601e
  1. 4
      README.md
  2. 28
      index.js
  3. 10
      query-stream.js

4
README.md

@ -147,6 +147,10 @@ Called when an update query is invoked. The `data.node` is also guaranteed to ha
Makes sure the initial bootstrap table has been built. You do not need to wait for this before querying.
#### `node.bootstrap([callback])`
Rebootstrap your node. Call this at regular intervals if you aren't doing any other queries.
#### `node.holepunch(peer, referrer, callback)`
UDP hole punch to another peer using the `referrer` as a STUN server.

28
index.js

@ -21,7 +21,6 @@ function DHT (opts) {
var self = this
this.concurrency = opts.concurrency || 16
this.bootstrap = [].concat(opts.bootstrap || []).map(parseAddr)
this.id = opts.id || crypto.randomBytes(32)
this.ephemeral = !!opts.ephemeral
this.nodes = new KBucket({localNodeId: this.id, arbiter: arbiter})
@ -38,6 +37,7 @@ function DHT (opts) {
this.socket.on('response', onresponse)
this.socket.on('close', onclose)
this._bootstrap = [].concat(opts.bootstrap || []).map(parseAddr)
this._queryId = this.ephemeral ? null : this.id
this._bootstrapped = false
this._pendingRequests = []
@ -55,7 +55,7 @@ function DHT (opts) {
}
process.nextTick(function () {
self._bootstrap()
self.bootstrap()
})
function rotateSecrets () {
@ -108,9 +108,6 @@ DHT.prototype.update = function (query, opts, cb) {
}
DHT.prototype._pingSome = function () {
var all = this.nodes.toArray()
if (!all.length) return
var cnt = this.inflightQueries > 2 ? 1 : 3
var oldest = this._bottom
@ -154,12 +151,10 @@ DHT.prototype._rotateSecrets = function () {
this._secrets[0] = secret
}
DHT.prototype._bootstrap = function () {
// TODO: i'm guessing we need to rebootstrap after some timeout
// TODO: check stats, to determine wheather to rerun?
DHT.prototype.bootstrap = function (cb) {
var self = this
if (!this.bootstrap.length) return process.nextTick(done)
if (!this._bootstrap.length) return process.nextTick(done)
var backgroundCon = Math.min(self.concurrency, Math.max(2, Math.floor(self.concurrency / 8)))
var qs = this.query({
@ -168,14 +163,21 @@ DHT.prototype._bootstrap = function () {
})
qs.on('data', update)
qs.on('error', noop) // noop this out as it'll bootstrap on subsequent runs
qs.on('error', onerror)
qs.on('end', done)
update()
function onerror (err) {
if (cb) cb(err)
}
function done () {
self._bootstrapped = true
self.emit('ready')
if (!self._bootstrapped) {
self._bootstrapped = true
self.emit('ready')
}
if (cb) cb()
}
function update () {
@ -402,8 +404,6 @@ DHT.prototype.listen = function (port, cb) {
this.socket.listen(port, cb)
}
function noop () {}
function encodePeer (peer) {
return peer && peers.encode([peer])
}

10
query-stream.js

@ -90,9 +90,9 @@ QueryStream.prototype._bootstrap = function () {
this._addPending({id: b.id, port: b.port, host: b.host}, null)
}
if (bootstrap.length < this._dht.bootstrap.length) {
for (i = 0; i < this._dht.bootstrap.length; i++) {
this._send(this._dht.bootstrap[i], true, false)
if (bootstrap.length < this._dht._bootstrap.length) {
for (i = 0; i < this._dht._bootstrap.length; i++) {
this._send(this._dht._bootstrap[i], true, false)
}
}
}
@ -145,6 +145,10 @@ QueryStream.prototype._callback = function (err, res, peer) {
if (this.destroyed) return
if (err) {
if (res && res.id) {
var node = this._dht.nodes.get(res.id)
if (node) this._dht._removeNode(node)
}
this.errors++
this.emit('warning', err)
this._readMaybe()

Loading…
Cancel
Save