1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-10 11:43:01 +03:00

Fix js parser handling big values not fast enough

Fixes #678
This commit is contained in:
Ruben Bridgewater
2015-11-22 16:58:51 +01:00
parent 8bf794fb36
commit 06a1bdd7b0
4 changed files with 156 additions and 79 deletions

View File

@@ -6,6 +6,8 @@ function JavascriptReplyParser() {
this.name = exports.name;
this._buffer = new Buffer(0);
this._offset = 0;
this._big_offset = 0;
this._chunks_size = 0;
this._buffers = [];
}
@@ -37,10 +39,6 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
}
return new Error(this._buffer.toString('utf-8', start, end));
} else if (type === 36) { // $
// set a rewind point, as the packet could be larger than the
// buffer in memory
offset = this._offset - 1;
packetHeader = this.parseHeader();
// packets with a size of -1 are considered null
@@ -52,6 +50,8 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
start = this._offset;
if (end > this._buffer.length) {
this._chunks_size = this._buffer.length - this._offset - 2;
this._big_offset = packetHeader;
throw new IncompleteReadBuffer('Wait for more data.');
}
@@ -60,6 +60,7 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
return this._buffer.slice(start, end);
} else if (type === 42) { // *
// set a rewind point, as the packet is larger than the buffer in memory
offset = this._offset;
packetHeader = this.parseHeader();
@@ -94,14 +95,11 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
};
JavascriptReplyParser.prototype.execute = function (buffer) {
var i = buffer.length - 1;
while (buffer[i] !== 0x0a) {
i--;
if (i < 1) {
this._buffers.push(buffer);
return;
}
if (this._chunks_size !== 0 && this._big_offset > this._chunks_size + buffer.length) {
this._buffers.push(buffer);
this._chunks_size += buffer.length;
return;
}
if (this._buffers.length !== 0) {
@@ -109,6 +107,8 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
this._buffers.push(buffer);
this._buffer = Buffer.concat(this._buffers);
this._buffers = [];
this._big_offset = 0;
this._chunks_size = 0;
} else if (this._offset >= this._buffer.length) {
this._buffer = buffer;
} else {
@@ -142,6 +142,8 @@ JavascriptReplyParser.prototype.run = function (buffer) {
this.send_reply(this._parseResult(type));
} else if (type !== 10 && type !== 13) {
// Reset the buffer so the parser can handle following commands properly
this._buffer = new Buffer(0);
var err = new Error('Protocol error, got "' + String.fromCharCode(type) + '" as reply type byte');
this.send_error(err);
}
@@ -169,7 +171,6 @@ JavascriptReplyParser.prototype._packetEndOffset = function () {
while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
offset++;
/* istanbul ignore if: activate the js parser out of memory test to test this */
if (offset >= this._buffer.length) {
throw new IncompleteReadBuffer('Did not see LF after NL reading multi bulk count (' + offset + ' => ' + this._buffer.length + ', ' + this._offset + ')');
}