Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge to head #1

Merged
merged 8 commits into from
Sep 29, 2023
25 changes: 23 additions & 2 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,33 @@ Client.create = function(serversStr, options) {
return new Client(servers, options);
};

// An overridable method you can use for determing
// server selection. Should return the server index
// in the list of servers on Client#servers.
//
// Example using node-hashring:
// ~~~~
// const memjs = require('memjs');
// const HashRing = require('node-hashring');
// const servers = ['localhost:11211', 'localhost:11212'];
// // build a map of server addresses to their index in the server list
// const serverMap = {};
// servers.forEach((server, index) => serverMap[server] = index);
// const client = memjs.Client.create(servers.join(','));
// // build the hashring
// const hashRing = new HashRing(servers);
// // override the getServer method
// client.getServer = (key) => serverMap[hashRing.findNode(key)];
// ~~~~
Client.prototype.getServer = function(key) {
return hashCode(key) % this.servers.length;
};

// Chooses the server to talk to by hashing the given key.
Client.prototype.server = function(key) {
// TODO(alevy): should use consistent hashing and/or allow swapping hashing
// mechanisms
var total = this.servers.length;
var origIdx = (total > 1) ? (hashCode(key) % total) : 0;
var origIdx = total > 1 ? this.getServer(key) : 0;
var idx = origIdx;
var serv = this.servers[idx];
while (serv.wakeupAt &&
Expand Down
5 changes: 4 additions & 1 deletion lib/memjs/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,16 @@ Server.prototype.sock = function(sasl, go) {

// setup error handler
self._socket.on('error', function(error) {
self.error(error);
});

self._socket.on('close', function() {
self.connected = false;
if (self.timeoutSet) {
self._socket.setTimeout(0);
self.timeoutSet = false;
}
self._socket = undefined;
self.error(error);
});

// setup connection timeout handler
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Amit Levy",
"name": "memjs",
"description": "A memcache client for node using the binary protocol and SASL authentication",
"version": "1.2.2",
"version": "1.3.1",
"license": "MIT",
"homepage": "http://github.com/memcachier/memjs",
"keywords": [
Expand Down