|
|
@ -2,6 +2,7 @@ const { Message, Holepunch, TYPE } = require('./messages') |
|
|
|
const blake2b = require('./blake2b') |
|
|
|
const peers = require('ipv4-peers') |
|
|
|
const sodium = require('sodium-universal') |
|
|
|
const speedometer = require('speedometer') |
|
|
|
|
|
|
|
const QUERY = Symbol('QUERY') |
|
|
|
const UPDATE = Symbol('UPDATE') |
|
|
@ -28,6 +29,7 @@ class IO { |
|
|
|
this._ticking = false |
|
|
|
this._tickInterval = setInterval(this._ontick.bind(this), 750) |
|
|
|
this._rotateInterval = setInterval(this._onrotate.bind(this), 300000) |
|
|
|
this._speed = speedometer() |
|
|
|
|
|
|
|
socket.on('message', this._onmessage.bind(this)) |
|
|
|
} |
|
|
@ -120,6 +122,10 @@ class IO { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_saturated () { |
|
|
|
return this._speed(0) >= this._ctx.concurrencyRPS && this.inflight.length >= this._ctx.concurrency |
|
|
|
} |
|
|
|
|
|
|
|
_finish (rid, err, val, peer) { |
|
|
|
const req = this._requests[rid] |
|
|
|
if (!req) return |
|
|
@ -135,7 +141,7 @@ class IO { |
|
|
|
|
|
|
|
req.callback(err, val, peer, req.message, req.peer, type) |
|
|
|
|
|
|
|
while (this._pending.length && this.inflight.length < this._ctx.concurrency) { |
|
|
|
while (this._pending.length && !this._saturated()) { |
|
|
|
const { message, peer, callback } = this._pending.shift() |
|
|
|
this._requestImmediately(message, peer, callback) |
|
|
|
} |
|
|
@ -143,7 +149,7 @@ class IO { |
|
|
|
|
|
|
|
_request (message, peer, callback) { |
|
|
|
// Should we wait to send?
|
|
|
|
if (this._pending.length || (this.inflight.length >= this._ctx.concurrency)) { |
|
|
|
if (this._pending.length || this._saturated()) { |
|
|
|
this._pending.push({ message, peer, callback }) |
|
|
|
} else { |
|
|
|
this._requestImmediately(message, peer, callback) |
|
|
@ -154,6 +160,8 @@ class IO { |
|
|
|
const rid = message.rid = this._free() |
|
|
|
const buffer = Message.encode(message) |
|
|
|
|
|
|
|
this._speed(1) |
|
|
|
|
|
|
|
const req = { |
|
|
|
rid, |
|
|
|
index: this.inflight.length, |
|
|
@ -189,7 +197,8 @@ class IO { |
|
|
|
const req = this.inflight[i] |
|
|
|
|
|
|
|
if (req.timeout === 2 && ++req.tries < TRIES) { |
|
|
|
this._retry(req) |
|
|
|
if (this._saturated()) req.tries-- |
|
|
|
else this._retry(req) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|