consistent_distribution.go 650 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. }