You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
650 B

5 years ago
5 years ago
  1. package broker
  2. import (
  3. "github.com/buraksezer/consistent"
  4. "github.com/cespare/xxhash"
  5. )
  6. type Member string
  7. func (m Member) String() string {
  8. return string(m)
  9. }
  10. type hasher struct{}
  11. func (h hasher) Sum64(data []byte) uint64 {
  12. return xxhash.Sum64(data)
  13. }
  14. func PickMember(members []string, key []byte) string {
  15. cfg := consistent.Config{
  16. PartitionCount: 9791,
  17. ReplicationFactor: 2,
  18. Load: 1.25,
  19. Hasher: hasher{},
  20. }
  21. cmembers := []consistent.Member{}
  22. for _, m := range members {
  23. cmembers = append(cmembers, Member(m))
  24. }
  25. c := consistent.New(cmembers, cfg)
  26. m := c.LocateKey(key)
  27. return m.String()
  28. }