From cb2bcc21c6bb476f9a927f097300a9ba13965b1b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 19 Dec 2015 02:52:51 -0800 Subject: [PATCH] prevent buffered _writes from binding thousands of events. --- lib/bcoin/peer.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 5730386d..85ef0241 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -244,13 +244,24 @@ Peer.prototype.destroy = function destroy() { // Private APIs Peer.prototype._write = function write(chunk) { + var self = this; + if (this.destroyed) return; if (!this.socket) { - return this.once('socket', function() { - this._write(chunk); - }); + if (!this._bchunks) { + this._bchunks = []; + this.once('socket', function() { + var chunks = self._bchunks; + delete self._bchunks; + chunks.forEach(function(chunk) { + self._write(chunk); + }); + }); + } + this._bchunks.push(chunk); + return; } if (NodeBuffer)