I'd like to have an opinion on this issue:
I use different clients for different databases, and so I select the database I want when the client is first created. Sometimes, connections get dropped, and so the selected database is no longer selected on reconnect. Of course, I could call select before each command, but that seems like a huge waste. So ideally, the command would be run every time the client connects.
Because of the way node_redis works though, commands sent from the "connect" event listener will be sent after commands which were sent while offline, this is because of the offline_queue, and the fact that ready is set after "connect" is emitted, so commands in the listener are queued!
The "ready" event is then emitted after all this, but at that point the offline commands are already sent, with the wrong database selected.
The solution I'm currently using, which seems to work is:
client.on('connect', function () {
client.send_anyway = true;
client.select(DB);
client.send_anyway = false;
});
This makes it so the select skips the ready-check, and sends the command before the offline_queue is sent.
Here's where this is all happening:
https://github.com/mranney/node_redis/blob/master/index.js#L187
A nice solution would be to emit "connect" after ready = true. Another solution would be to have another event there, like "connected", which gets emitted after we're ready, but before the queue is sent.
Thoughts?
I'd like to have an opinion on this issue:
I use different clients for different databases, and so I
selectthe database I want when the client is first created. Sometimes, connections get dropped, and so the selected database is no longer selected on reconnect. Of course, I could callselectbefore each command, but that seems like a huge waste. So ideally, the command would be run every time the client connects.Because of the way node_redis works though, commands sent from the
"connect"event listener will be sent after commands which were sent while offline, this is because of theoffline_queue, and the fact thatreadyis set after"connect"is emitted, so commands in the listener are queued!The
"ready"event is then emitted after all this, but at that point the offline commands are already sent, with the wrong database selected.The solution I'm currently using, which seems to work is:
This makes it so the
selectskips the ready-check, and sends the command before the offline_queue is sent.Here's where this is all happening:
https://github.com/mranney/node_redis/blob/master/index.js#L187
A nice solution would be to emit
"connect"afterready = true. Another solution would be to have another event there, like"connected", which gets emitted after we're ready, but before the queue is sent.Thoughts?