Skip to content

Commit 1af792c

Browse files
author
Ruben Bridgewater
committed
Fix parser not working with huge buffers (e.g. 300mb)
Closes #8
1 parent 9f8f199 commit 1af792c

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v.2.0.3 - 17 Jun, 2016
2+
3+
Bugfixes
4+
5+
- Fixed parser not working with huge buffers (e.g. 300 MB)
6+
17
## v.2.0.2 - 08 Jun, 2016
28

39
Bugfixes

lib/parser.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ function decreaseBufferPool () {
313313
// Balance between increasing and decreasing the bufferPool
314314
if (counter === 1 || notDecreased > counter * 2) {
315315
// Decrease the bufferPool by 16kb by removing the first 16kb of the current pool
316-
bufferPool = bufferPool.slice(16 * 1024, bufferPool.length)
316+
bufferPool = bufferPool.slice(Math.floor(bufferPool.length / 10), bufferPool.length)
317317
} else {
318318
notDecreased++
319319
counter--
@@ -338,12 +338,17 @@ function concatBuffer (parser, length) {
338338
length -= parser.offset
339339
if (bufferPool.length < length + bufferOffset) {
340340
// Increase the bufferPool size by three times the current needed length
341-
bufferPool = new Buffer(length * 3 + bufferOffset)
341+
var multiplier = 3
342+
if (bufferOffset > 1024 * 1024 * 200) {
343+
bufferOffset = 1024 * 1024 * 50
344+
multiplier = 2
345+
}
346+
bufferPool = new Buffer(length * multiplier + bufferOffset)
342347
bufferOffset = 0
343348
counter++
344349
pos = 0
345350
if (interval === null) {
346-
interval = setInterval(decreaseBufferPool, 30)
351+
interval = setInterval(decreaseBufferPool, 50)
347352
}
348353
}
349354
list[0].copy(bufferPool, pos, parser.offset, list[0].length)
@@ -369,10 +374,10 @@ JavascriptRedisParser.prototype.execute = function (buffer) {
369374
} else if (this.bigStrSize === 0) {
370375
var oldLength = this.buffer.length
371376
var remainingLength = oldLength - this.offset
372-
var bufferPool = new Buffer(remainingLength + buffer.length)
373-
this.buffer.copy(bufferPool, 0, this.offset, oldLength)
374-
buffer.copy(bufferPool, remainingLength, 0, buffer.length)
375-
this.buffer = bufferPool
377+
var newBuffer = new Buffer(remainingLength + buffer.length)
378+
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
379+
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
380+
this.buffer = newBuffer
376381
this.offset = 0
377382
} else if (this.totalChunkSize + buffer.length >= this.bigStrSize) {
378383
this.bufferCache.push(buffer)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"istanbul": "^0.4.0",
3636
"standard": "^7.0.1",
3737
"mocha": "^2.3.2",
38-
"hiredis": "^0.4.1"
38+
"hiredis": "^0.5.0"
3939
},
4040
"author": "Ruben Bridgewater",
4141
"license": "MIT",

0 commit comments

Comments
 (0)