Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 722 Bytes

README.md

File metadata and controls

34 lines (27 loc) · 722 Bytes

go-consistent

go consistent hashing library

It is NOT concurrency safe.

Install

go get github.com/xiaonanln/go-consistent

Usage

import "github.com/xiaonanln/go-consistent"

// Create Consistent Hashing
c := consistent.New()
// Hash returns ErrNoHost if there are no hosts
c.Hash("key") // returns "", consistent.ErrNoHost
// Add adds a new hash
c.Add("host1")
c.Hash("key") // returns "host1", nil 

c.Add("host2")
c.Hash("key") // returns "host1"/"host2", nil

// SetReplica changes replica. The defualt replica is 20
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)