From 6c118c311f331e8ae4032bcf6d6babc1f04402cc Mon Sep 17 00:00:00 2001 From: Brian Rossmajer Date: Thu, 7 Apr 2016 23:05:56 -0400 Subject: [PATCH] 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 --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 1e1288f..5e39806 100644 --- a/README.md +++ b/README.md @@ -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,