2016-08-15 22:21:22 +08:00
|
|
|
# hashring
|
2016-08-15 22:52:09 +08:00
|
|
|
|
|
|
|
A golang consistent hashring
|
|
|
|
|
|
|
|
Install
|
|
|
|
===
|
|
|
|
|
|
|
|
go get github.com/g4zhuj/hashring
|
|
|
|
|
|
|
|
Usage
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
// virtualSpots means virtual spots created by each node
|
2018-04-09 11:34:07 +08:00
|
|
|
nodeWeight := make(map[string]int)
|
|
|
|
nodeWeight["node1"] = 1
|
|
|
|
nodeWeight["node2"] = 1
|
|
|
|
nodeWeight["node3"] = 2
|
|
|
|
vitualSpots := 100
|
|
|
|
hash := NewHashRing(virtualSpots)
|
2016-08-15 22:52:09 +08:00
|
|
|
|
|
|
|
|
2018-04-09 11:34:07 +08:00
|
|
|
//add nodes
|
|
|
|
hash.AddNodes(nodeWeight)
|
2016-08-15 22:52:09 +08:00
|
|
|
|
2018-04-09 11:34:07 +08:00
|
|
|
//remove node
|
|
|
|
hash.RemoveNode("node3")
|
2016-08-15 22:52:09 +08:00
|
|
|
|
|
|
|
|
2018-04-09 11:34:07 +08:00
|
|
|
//add node
|
|
|
|
hash.AddNode("node3", 3)
|
2016-08-15 22:52:09 +08:00
|
|
|
|
|
|
|
|
2018-04-09 11:34:07 +08:00
|
|
|
//get key's node
|
|
|
|
node := hash.GetNode("key")
|
2016-08-15 22:52:09 +08:00
|
|
|
|
|
|
|
```
|