consistent-hash-js
consistent-hash-js copied to clipboard
Not really consistent
this implementation has bugs below is test case
test('Should getHash - be same always be same', async () => { for (let i = 0; i < 100; i++) { const ring = ['aaaa', 'bbbb', 'cccc']; const consistentHash = new ConsistentHash(ring); expect(consistentHash.get('123123123')).toEqual('cccc'); } });
Hi, sorry for the delay. The constructor does not support an array argument. I think what you're trying to do accomplish is:
const assert = require('assert');
for (let i = 0; i < 100; i++) {
const hash = new ConsistentHash({ distribution: 'uniform' });
['aaaa', 'bbbb', 'cccc'].forEach((node) => hash.add(node));
assert.strictEqual(hash.get('123123123'), 'bbbb');
}
It might be convenient to have a nodes
option though.
I added a nodes
option to constructor options; if that works for you I'll publish it as version 1.3.0. I also added the above sample code to the unit tests to verify that many uniform hash rings all return the same node to handle the same input.
Still need to specify distribution: 'uniform'
though, the default is random
(for historical reasons), and random distribution hash rings will return different nodes for the same input.
Do you plan on publishing 1.2.0 (and any other future versions) to npm?
thanks for the nudge, published 1.2.1