Permalink
Browse files

Fix tls buffer allocation. Exhaust buffer before allocating new one.

  • Loading branch information...
1 parent e8e4604 commit 3b8e71cb11b3e55f4e4d27c5f8b4a741922f61ab @yosefd yosefd committed Mar 23, 2012
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/tls.js
View
@@ -326,12 +326,16 @@ CryptoStream.prototype._push = function() {
}
while (!this._paused) {
- var bytesRead = 0;
var chunkBytes = 0;
- var pool = new Buffer(16 * 4096); // alloc every time?
+ if (!this._pool || (this._poolStart >= this._poolEnd)) {
+ this._pool = new Buffer(16 * 4096);
+ this._poolStart = 0;
+ this._poolEnd = this._pool.length;
+ }
+ var start = this._poolStart;
do {
- chunkBytes = this._pusher(pool, bytesRead, pool.length - bytesRead);
+ chunkBytes = this._pusher(this._pool, this._poolStart, this._poolEnd - this._poolStart);
if (this.pair.ssl && this.pair.ssl.error) {
this.pair.error();
@@ -341,10 +345,12 @@ CryptoStream.prototype._push = function() {
this.pair.maybeInitFinished();
if (chunkBytes >= 0) {
- bytesRead += chunkBytes;
+ this._poolStart += chunkBytes;
}
- } while (chunkBytes > 0 && bytesRead < pool.length);
+ } while (chunkBytes > 0 && this._poolStart < this._poolEnd);
+
+ var bytesRead = this._poolStart - start;
assert(bytesRead >= 0);
@@ -356,7 +362,7 @@ CryptoStream.prototype._push = function() {
return;
}
- var chunk = pool.slice(0, bytesRead);
+ var chunk = this._pool.slice(start, this._poolStart);
if (this === this.pair.cleartext) {
debug('cleartext emit "data" with ' + bytesRead + ' bytes');
@@ -372,7 +378,7 @@ CryptoStream.prototype._push = function() {
}
// Optimization: emit the original buffer with end points
- if (this.ondata) this.ondata(pool, 0, bytesRead);
+ if (this.ondata) this.ondata(this._pool, start, bytesRead);
}
};

0 comments on commit 3b8e71c

Please sign in to comment.