项目作者: xiaonanln

项目描述 :
Go consistent hashing library
高级语言: Go
项目地址: git://github.com/xiaonanln/go-consistent.git
创建时间: 2018-08-20T13:15:04Z
项目社区:https://github.com/xiaonanln/go-consistent

开源协议:MIT License

下载


go-consistent

go consistent hashing library

It is NOT concurrency safe.

Install

  1. go get github.com/xiaonanln/go-consistent

Usage

  1. import "github.com/xiaonanln/go-consistent"
  2. // Create Consistent Hashing
  3. c := consistent.New()
  4. // Hash returns ErrNoHost if there are no hosts
  5. c.Hash("key") // returns "", consistent.ErrNoHost
  6. // Add adds a new hash
  7. c.Add("host1")
  8. c.Hash("key") // returns "host1", nil
  9. c.Add("host2")
  10. c.Hash("key") // returns "host1"/"host2", nil
  11. // SetReplica changes replica. The defualt replica is 20
  12. c.SetReplica(100) // reset replica from 20 to 100

Time complexity

Assuming N is number of hosts:

  • Add = O(N)
  • Hash = O(log N)
  • SetReplica = O(N)