node-cluster-socket.io
node-cluster-socket.io copied to clipboard
worker_index doesn't work for ipv6
With an ip address of ::ffff:127.0.0.1
worker_index returns NaN
I changed it to :
var worker_index = function(ip, len) {
var s = '';
for (var i = 0, _len = ip.length; i < _len; i++) {
if (parseInt(ip[i], 10)) {
s += ip[i];
}
}
return Number(s) % len || 0;
};
Not sure how bad that effects performance.
Why not if (ip[i] >= '0' && ip[i] <= '9')
instead?
Yeah I guess that works as well not sure which one is faster.
Although that will skip [a-f] on ipv6 addresses which might effect distribution. (mine does skip them as well) :laughing:
We'll have to adjust the other hashing functions to also work for IPv6 and then compare...
As a solution to the IPv6 problem, I'm using the following code:
function getWorker(ip, len) {
var _ip = ip.split(/['.'|':']/),
arr = [];
for (el in _ip) {
if (_ip[el] == '') {
arr.push(0);
} else {
arr.push(parseInt(_ip[el], 16));
}
return Number(arr.join('')) % len;
}
Please let me know your thoughts
Guys, have a look at: https://github.com/uqee/sticky-cluster for ipv6 support.
@uqee we can easily make the example code in this module work with IPv6 if we replace the embedded hashing function with string-hash
like in your project and then I believe performance will be comparable.
@elad, yes, you're definitely right
What about this one? Would it damage the performance?
var worker_index = function(ip, len) {
var s = '';
for (var i = 0; i < ip.length; i++) {
if (!isNaN(ip[i])) {
s += ip[i];
}
}
return Number(s) % len;
};
I use that code to handle IPv4 and IPv6. Maybe I missed something but it seems much faster.
var worker_index = function(ip, len) {
return Number('0' + s.replace(/[abcdef:.]/g, '')) % len;
};
http://jsperf.com/ip-string-replace
Was there an official answer to this btw?