Skip to content

Commit

Permalink
Fix bug where subscribe commands would not handle redis-server startu…
Browse files Browse the repository at this point in the history
…p error properly.
  • Loading branch information
mranney committed Jan 18, 2011
1 parent fc5968c commit c1b5587
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions changelog.md
@@ -1,6 +1,10 @@
Changelog
=========

## v0.5.1 - January 18, 2011

Fix bug where subscribe commands would not handle redis-server startup error properly.

## v0.5.0 - December 29, 2010

Some bug fixes:
Expand Down
22 changes: 13 additions & 9 deletions index.js
Expand Up @@ -191,7 +191,7 @@ RedisClient.prototype.connection_gone = function (why) {
}

if (exports.debug_mode) {
console.log("Retry conneciton in " + self.retry_delay + " ms");
console.log("Retry connection in " + self.retry_delay + " ms");
}
self.attempts += 1;
self.emit("reconnecting", {
Expand All @@ -210,7 +210,7 @@ RedisClient.prototype.connection_gone = function (why) {

RedisClient.prototype.on_data = function (data) {
if (exports.debug_mode) {
console.log("on_data: " + data.toString());
console.log("net read fd " + this.stream.fd + ": " + data.toString());
}

try {
Expand Down Expand Up @@ -241,7 +241,7 @@ RedisClient.prototype.return_error = function (err) {
});
}
} else {
console.log("node_redis: no callback to send error: " + util.inspect(err));
console.log("node_redis: no callback to send error: " + err.message);
// this will probably not make it anywhere useful, but we might as well throw
throw err;
}
Expand All @@ -255,7 +255,7 @@ RedisClient.prototype.return_reply = function (reply) {
this.emit("idle");
}

if (command_obj) {
if (command_obj && !command_obj.sub_command) {
if (typeof command_obj.callback === "function") {
// HGETALL special case replies with keyed Buffers
if (reply && 'hgetall' === command_obj.command.toLowerCase()) {
Expand Down Expand Up @@ -301,6 +301,8 @@ RedisClient.prototype.return_reply = function (reply) {
} else {
throw new Error("subscriptions are active but got an invalid reply: " + reply);
}
} else {
throw new Error("node_redis command queue state error. If you can reproduce this, please report it.");
}
};

Expand Down Expand Up @@ -336,7 +338,8 @@ RedisClient.prototype.send_command = function () {
command_obj = {
command: command,
args: args,
callback: callback
callback: callback,
sub_command: false
};

if (! this.connected) {
Expand All @@ -351,29 +354,30 @@ RedisClient.prototype.send_command = function () {
if (this.subscriptions === false && exports.debug_mode) {
console.log("Entering pub/sub mode from " + command);
}
command_obj.sub_command = true;
this.subscriptions = true;
} else {
if (command === "quit") {
this.closing = true;
} else if (this.subscriptions === true) {
throw new Error("Connection in pub/sub mode, only pub/sub commands may be used");
}
this.command_queue.push(command_obj);
}
this.command_queue.push(command_obj);
this.commands_sent += 1;

elem_count = 1;
buffer_args = false;

elem_count += args.length;
// Probably should just scan this like a normal person
// Probably should just scan this like a normal person. This is clever, but might be slow.
buffer_args = args.some(function (arg) {
// this is clever, but might be slow
return arg instanceof Buffer;
});

// Always use "Multi bulk commands", but if passed any Buffer args, then do multiple writes, one for each arg
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
// Also, why am I putting user documentation in the library source code?

command_str = "*" + elem_count + "\r\n$" + command.length + "\r\n" + command + "\r\n";

Expand All @@ -391,7 +395,7 @@ RedisClient.prototype.send_command = function () {
command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n";
}
if (exports.debug_mode) {
console.log("send command: " + command_str);
console.log("send fd " + this.stream.fd + ": " + command_str);
}
stream.write(command_str);
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,5 +1,5 @@
{ "name" : "redis",
"version" : "0.5.0",
"version" : "0.5.1",
"description" : "Redis client library",
"author": "Matt Ranney <mjr@ranney.com>",
"contributors": [
Expand Down

0 comments on commit c1b5587

Please sign in to comment.