Skip to content

Commit 621c511

Browse files
author
Ruben Bridgewater
committed
Return parser data async
1 parent 97db227 commit 621c511

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ RedisClient.prototype.install_stream_listeners = function() {
8686
});
8787

8888
this.stream.on("data", function (buffer_from_socket) {
89-
self.on_data(buffer_from_socket);
89+
// The data.toString() has a significant impact on big chunks and therefor this should only be used if necessary
90+
// debug("Net read " + this.address + " id " + this.connection_id + ": " + data.toString());
91+
self.reply_parser.execute(buffer_from_socket);
9092
});
9193

9294
this.stream.on("error", function (msg) {
@@ -273,8 +275,18 @@ RedisClient.prototype.init_parser = function () {
273275
this.reply_parser = new this.parser_module.Parser({
274276
return_buffers: self.options.return_buffers || self.options.detect_buffers || false
275277
});
276-
this.reply_parser.send_error = this.return_error.bind(self);
277-
this.reply_parser.send_reply = this.return_reply.bind(self);
278+
// Important: Only send results / errors async.
279+
// That way the result / error won't stay in a try catch block and catch user things
280+
this.reply_parser.send_error = function (data) {
281+
process.nextTick(function() {
282+
this.return_error(data);
283+
}.bind(this));
284+
}.bind(this);
285+
this.reply_parser.send_reply = function (data) {
286+
process.nextTick(function() {
287+
this.return_reply(data);
288+
}.bind(this));
289+
}.bind(this);
278290
};
279291

280292
RedisClient.prototype.on_ready = function () {
@@ -488,33 +500,22 @@ RedisClient.prototype.connection_gone = function (why) {
488500
this.retry_timer = setTimeout(retry_connection, this.retry_delay, this);
489501
};
490502

491-
RedisClient.prototype.on_data = function (data) {
492-
// The data.toString() has a significant impact on big chunks and therefor this should only be used if necessary
493-
// debug("Net read " + this.address + " id " + this.connection_id + ": " + data.toString());
494-
495-
try {
496-
this.reply_parser.execute(data);
497-
} catch (err) {
498-
// This is an unexpected parser problem, an exception that came from the parser code itself.
499-
// Parser should emit "error" events if it notices things are out of whack.
500-
// Callbacks that throw exceptions will land in return_reply(), below.
501-
// TODO - it might be nice to have a different "error" event for different types of errors
502-
this.emit("error", err);
503-
}
504-
};
505-
506503
RedisClient.prototype.return_error = function (err) {
507504
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
508-
err.command_used = command_obj.command.toUpperCase();
505+
if (command_obj.command && command_obj.command.toUpperCase) {
506+
err.command_used = command_obj.command.toUpperCase();
507+
}
509508

510509
if (this.pub_sub_mode === false && queue_len === 0) {
511510
this.command_queue = new Queue();
512511
this.emit("idle");
513512
}
513+
514514
if (this.should_buffer && queue_len <= this.command_queue_low_water) {
515515
this.emit("drain");
516516
this.should_buffer = false;
517517
}
518+
518519
if (command_obj.callback) {
519520
command_obj.callback(err);
520521
} else {

0 commit comments

Comments
 (0)