Skip to content

Commit

Permalink
Refactor out 'fullReady' waiting
Browse files Browse the repository at this point in the history
  • Loading branch information
tysonmote committed Sep 23, 2020
1 parent aa3b5ec commit 900a426
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/RedisClustr.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,10 @@ RedisClustr.prototype.getSlots = function(cb) {
}

if (!self.fullReady) {
var ready = 0;
for (var i = 0; i < seenClients.length; i++) {
var c = self.connections[seenClients[i]];
if (c.ready) {
if (++ready === seenClients.length) {
self.fullReady = true;
self.emit('fullReady');
}
continue;
}
c.once('ready', function() {
if (++ready === seenClients.length) {
self.fullReady = true;
self.emit('fullReady');
}
});
}
self._waitUntilAllReady(seenClients, function() {
self.fullReady = true;
self.emit('fullReady');
})
}
});
};
Expand Down Expand Up @@ -293,6 +280,33 @@ RedisClustr.prototype._buildSlotMap = function(slots) {
return [slotList, Array.from(seenClients)]
}

/**
* Call the given callback when all of the given Redis connections have reached
* the 'ready' state.
* @date 2020-09-23
* @param {array} clients Clients to wait for, as "host:port" strings.
* @param {function} cb Callback to call when all given clients are
* ready.
*/
RedisClustr.prototype._waitUntilAllReady = function(clients, cb) {
const self = this;

var ready = 0;

for (var i = 0; i < clients.length; i++) {
var c = self.connections[clients[i]];

if (c.ready) {
if (++ready === clients.length) cb();
continue;
}

c.once('ready', function() {
if (++ready === clients.length) cb();
});
}
}

/**
* Select a Redis client for the given key and conf
* @date 2015-11-23
Expand Down

0 comments on commit 900a426

Please sign in to comment.