Skip to content

Commit

Permalink
Merge pull request #140 from kawanet/single-server
Browse files Browse the repository at this point in the history
avoid calculating hashCode for single server
  • Loading branch information
alevy authored Dec 17, 2020
2 parents 3155b9a + 6f5bd14 commit 368a57e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ Client.create = function(serversStr, options) {
Client.prototype.server = function(key) {
// TODO(alevy): should use consistent hashing and/or allow swapping hashing
// mechanisms
var origIdx = hashCode(key) % this.servers.length;
var total = this.servers.length;
var origIdx = (total > 1) ? (hashCode(key) % total) : 0;
var idx = origIdx;
var serv = this.servers[idx];
while (serv.wakeupAt &&
serv.wakeupAt > Date.now()) {
idx = (idx + 1) % this.servers.length;
idx = (idx + 1) % total;
if (idx === origIdx) {
return null;
}
Expand Down

0 comments on commit 368a57e

Please sign in to comment.