Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xiaonanln/go-consistent

Go consistent hashing library
https://github.com/xiaonanln/go-consistent

consistent-hashing consistent-hashing-library murmur

Last synced: about 1 month ago
JSON representation

Go consistent hashing library

Awesome Lists containing this project

README

        

# go-consistent
go consistent hashing library

**It is NOT concurrency safe.**

# Install
```bash
go get github.com/xiaonanln/go-consistent
```

# Usage

```go
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)`