Skip to content

Commit 6c118c3

Browse files
Noting when client.duplicate() might be used
Explicitly pointing out that when blocking and non-blocking functions are used on the same connection, unexpected results can occur
1 parent 7ffba03 commit 6c118c3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,38 @@ the second word as first parameter:
618618

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

621+
An example of when to use duplicate() would be to accomodate the connection-
622+
blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands
623+
are used on the same redisClient instance as non-blocking commands, the
624+
non-blocking ones may be queued up until after the blocking ones finish.
625+
626+
var Redis=require('redis');
627+
var client = Redis.createClient();
628+
var get = function() {
629+
console.log("get called");
630+
client.get("any_key",function() { console.log("get returned"); });
631+
setTimeout( get, 1000 );
632+
};
633+
var brpop = function() {
634+
console.log("brpop called");
635+
client.brpop("nonexistent", 5, function() {
636+
console.log("brpop return");
637+
setTimeout( brpop, 1000 );
638+
});
639+
};
640+
get();
641+
brpop();
642+
643+
These two repeating functions will interfere with each other -- the `get`s will
644+
not return until after the `brpop` returns. This can be fixed by keeping the
645+
blocking calls separate using `client.duplicate()`, eg:
646+
647+
...
648+
var clientBlocking = client.duplicate();
649+
var brpop = function() {
650+
console.log("brpop called");
651+
clientBlocking.brpop( ...
652+
621653
## client.send_command(command_name[, [args][, callback]])
622654

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

0 commit comments

Comments
 (0)