-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (50 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';
var HashRing = require('hashring');
var _ = require('underscore');
var SDC = require('statsd-client');
function ProxyWrapper(options, callback)
{
if (!options || !options.nodes || !options.nodes.length)
return callback && callback(new Error('Invalid options object'))
this.ring = new HashRing();
this.nodeMap = {};
var nodeOptions, node, nodeAddr;
for (var i = 0; i < options.nodes.length; i++)
{
nodeOptions = _.clone(options);
delete nodeOptions.nodes;
nodeOptions.host = options.nodes[i].host;
nodeOptions.port = options.nodes[i].port;
node = new SDC(nodeOptions)
nodeAddr = nodeOptions.host + ':' + nodeOptions.port;
// ring.add('127.0.0.7').remove('127.0.0.1');
this.ring.add(nodeAddr);
this.nodeMap[nodeAddr] = node;
}
callback && callback();
}
ProxyWrapper.prototype.getNodeFromKey = function getNodeFromKey(key)
{
return this.nodeMap[this.ring.get(key)];
};
var methods = [
'increment',
'decrement',
'counter',
'gaugeDelta',
'gauge',
'set',
'raw',
];
for (var i = 0; i < methods.length; i++)
{
ProxyWrapper.prototype[methods[i]] = (function (method)
{
return function (key)
{
var node = this.getNodeFromKey(key);
return node[method].apply(node, arguments);
};
})(methods[i]);
}
module.exports = ProxyWrapper;