Skip to content

Instantly share code, notes, and snippets.

@songjiayang
Created March 8, 2018 03:27
Show Gist options
  • Save songjiayang/9fbd0c2f92813b7c5077f91c051c1afe to your computer and use it in GitHub Desktop.
Save songjiayang/9fbd0c2f92813b7c5077f91c051c1afe to your computer and use it in GitHub Desktop.
package main
import (
"crypto/md5"
"fmt"
)
func main() {
nodes := []string{
"192.168.1.1:9090", "192.168.1.2:9090", "192.168.1.3:9090", "192.168.1.4:9090", "192.168.1.5:9090",
"192.168.1.6:9090", "192.168.1.7:9090", "192.168.1.8:9090", "192.168.1.9:9090", "192.168.1.10:9090",
}
for _, ip := range nodes {
mod := sum64(md5.Sum([]byte(ip))) % 4
fmt.Printf("%s mode is %d \n", ip, mod)
}
}
func sum64(hash [md5.Size]byte) uint64 {
var s uint64
for i, b := range hash {
shift := uint64((md5.Size - i - 1) * 8)
s |= uint64(b) << shift
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment