Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
push:
branches: [ master, main, develop ]
pull_request:
branches: [ master, main, develop ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 22.x, 24.x]
steps:
- uses: actions/checkout@v7
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
cache: npm
- name: Install dependencies and build addon
run: npm ci
- name: Run tests
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
node_modules
**.swp
!.gitignore
!.github
npm-debug.log
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
TESTS = 'test/test_distribution.js'

test:
@NODE_ENV=test expresso \
$(TESTS)
@npm test

.PHONY: test
42 changes: 24 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
## node-hash-ring - Consistent Hashing C++ Add-on for node.js
---

See this [blog post](http://ngchi.wordpress.com/2010/08/23/towards-auto-sharding-in-your-node-js-app/) for more information.
See this [blog post](http://ngchi.wordpress.com/2010/08/23/towards-auto-sharding-in-your-node-js-app/) for background on the algorithm.

### Installation
Via npm:

npm install hash_ring
Via npm:

Via git:
npm install @movable/hash_ring

git clone http://github.com/bnoguchi/node-hash-ring.git
The native addon is compiled on install via `node-gyp` (invoked automatically), so a C++ toolchain is required.

#### node < 0.8.0
cd node-hash-ring/src
node-waf configure build
From source:

#### node >= 0.8.0
git clone https://github.com/movableink/node-hash-ring.git
cd node-hash-ring
node-gyp configure build
npm install

### Example

```javascript
var HashRing = require("./lib/hash_ring");
const { HashRing } = require("@movable/hash_ring");

// Create a cluster of 3 servers weighted so that 127.0.0.2:8080 stores twice as many
// Create a cluster of 3 servers weighted so that 127.0.0.2:8080 stores twice as many
// keys as 127.0.0.1:8080, and 127.0.0.3:8080 stores 4x as many keys as 127.0.0.1:8080
// and 2x as many keys as 127.0.0.2:8080
var ring = new HashRing({"127.0.0.1:8080": 1, "127.0.0.2:8080": 2, "127.0.0.3:8080":4});
console.log(ring.getNode("users:102") ); // Should be 127.0.0.3:8080
const ring = new HashRing({ "127.0.0.1:8080": 1, "127.0.0.2:8080": 2, "127.0.0.3:8080": 4 });

console.log(ring.getNode("users:102")); // Should be 127.0.0.3:8080
console.log(ring.getBuckets()); // ['127.0.0.1:8080', '127.0.0.2:8080', '127.0.0.3:8080']
```

An optional second argument controls the number of replica points per bucket (precision):

```javascript
const ring = new HashRing({ a: 5, b: 3 }, 500);
```

See [./test/test_distribution.js](https://github.com/bnoguchi/node-hash-ring/test/test_distribution.js) for another example:
node test/test_distribution.js
TypeScript definitions are bundled ([index.d.ts](index.d.ts)).

### Tests

To run the tests:
Tests run with [mocha](https://mochajs.org/):

$ make test
npm test

### License
MIT License

---
### Author
Brian Noguchi

Maintained by [Movable Ink](https://github.com/movableink).
Loading