Skip to content

Commit

Permalink
Noting when client.duplicate() might be used
Browse files Browse the repository at this point in the history
Explicitly pointing out that when blocking and non-blocking functions are used on the same connection, unexpected results can occur
  • Loading branch information
BrianRossmajer committed Apr 8, 2016
1 parent 7ffba03 commit 6c118c3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,38 @@ the second word as first parameter:

Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option.

An example of when to use duplicate() would be to accomodate the connection-
blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands
are used on the same redisClient instance as non-blocking commands, the
non-blocking ones may be queued up until after the blocking ones finish.

var Redis=require('redis');
var client = Redis.createClient();
var get = function() {
console.log("get called");
client.get("any_key",function() { console.log("get returned"); });
setTimeout( get, 1000 );
};
var brpop = function() {
console.log("brpop called");
client.brpop("nonexistent", 5, function() {
console.log("brpop return");
setTimeout( brpop, 1000 );
});
};
get();
brpop();

These two repeating functions will interfere with each other -- the `get`s will
not return until after the `brpop` returns. This can be fixed by keeping the
blocking calls separate using `client.duplicate()`, eg:

...
var clientBlocking = client.duplicate();
var brpop = function() {
console.log("brpop called");
clientBlocking.brpop( ...

## client.send_command(command_name[, [args][, callback]])

All Redis commands have been added to the `client` object. However, if new commands are introduced before this library is updated,
Expand Down

0 comments on commit 6c118c3

Please sign in to comment.