consistent hashring for golang(>=1.18)
go get -u github.com/lockp111/consistent-hashring
func main(){
type NodeInfo struct{
Host string
Name string
}
// new manager and set base virtual node count for every node
manager = NewManager[NodeInfo](100)
}
func main(){
nodeInfo := &NodeInfo{
Host: "127.0.0.1"
Name: "localhost"
}
node := NewNode(node.Name, nodeInfo)
// change weight, default 1
node.SetWeight(10)
manager.Add(node)
manager.Remove(nodeInfo.Name)
}
func main(){
node, ok := manager.GetNode("localhost")
nodes := manager.GetNodes()
}
func main(){
slot, ok := manager.FindOne("xxxxx")
nodeInfo := slot.GetValue()
}
func main(){
slots := manager.FindNext("xxxxx", 10) //or slots := manager.FindPrev("xxxxx", 10)
for _, slot := range slots {
nodeInfo := slot.GetValue()
}
}