Skip to content

Implementing Consistent Hashing in Golang with Virtual Nodes

Notifications You must be signed in to change notification settings

xinst/consistenthash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

consistenthash

License GoDoc Go Report Card Build Status Version Gitter

Implementing Consistent Hashing in Golang with Virtual Nodes

package main

import (
    "fmt"
    "github.com/xinst/consistenthash"
    "strings"
)

func main() {
    constHash := consistenthash.NewConsistentHash(100)

    objects := make(map[string]int)
    objects["node1"] = 0
    objects["node2"] = 0
    objects["node3"] = 0
    objects["node4"] = 0

    for k := range objects {
        constHash.AddNode(k)
    }

    for index := 0; index < 10000; index++ {
        node := constHash.GetRandomSuitNode()
        if index == 984 {
            objects["node5"] = 0
            constHash.AddNode("node5")
        }
        if index == 10 {
            constHash.RemoveNode("node3")
        }
        if node != nil {
            names := strings.Split(node.Name(), "-")
            objects[names[0]]++
        }
    }
    fmt.Println("nodes visit result:")
    for k, v := range objects {
        fmt.Printf("%s:%d\n", k, v)
    }
}

#test result

nodes visit result: node2:2045 node3:1 node4:2715 node5:2202 node1:3037

About

Implementing Consistent Hashing in Golang with Virtual Nodes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages