Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serialx/hashring
Consistent hashing "hashring" implementation in golang (using the same algorithm as libketama)
https://github.com/serialx/hashring
Last synced: 6 days ago
JSON representation
Consistent hashing "hashring" implementation in golang (using the same algorithm as libketama)
- Host: GitHub
- URL: https://github.com/serialx/hashring
- Owner: serialx
- License: mit
- Created: 2014-08-10T08:07:38.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T00:35:10.000Z (over 4 years ago)
- Last Synced: 2024-07-12T02:06:49.287Z (4 months ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 564
- Watchers: 19
- Forks: 95
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
hashring
============================Implements consistent hashing that can be used when
the number of server nodes can increase or decrease (like in memcached).
The hashing ring is built using the same algorithm as libketama.This is a port of Python hash_ring library
in Go with the extra methods to add and remove nodes.Using
============================Importing ::
```go
import "github.com/serialx/hashring"
```Basic example usage ::
```go
memcacheServers := []string{"192.168.0.246:11212",
"192.168.0.247:11212",
"192.168.0.249:11212"}ring := hashring.New(memcacheServers)
server, _ := ring.GetNode("my_key")
```To fulfill replication requirements, you can also get a list of servers that should store your key.
```go
serversInRing := []string{"192.168.0.246:11212",
"192.168.0.247:11212",
"192.168.0.248:11212",
"192.168.0.249:11212",
"192.168.0.250:11212",
"192.168.0.251:11212",
"192.168.0.252:11212"}replicaCount := 3
ring := hashring.New(serversInRing)
server, _ := ring.GetNodes("my_key", replicaCount)
```Using weights example ::
```go
weights := make(map[string]int)
weights["192.168.0.246:11212"] = 1
weights["192.168.0.247:11212"] = 2
weights["192.168.0.249:11212"] = 1ring := hashring.NewWithWeights(weights)
server, _ := ring.GetNode("my_key")
```Adding and removing nodes example ::
```go
memcacheServers := []string{"192.168.0.246:11212",
"192.168.0.247:11212",
"192.168.0.249:11212"}ring := hashring.New(memcacheServers)
ring = ring.RemoveNode("192.168.0.246:11212")
ring = ring.AddNode("192.168.0.250:11212")
server, _ := ring.GetNode("my_key")
```