hashring

package module
v0.0.0-...-eaea2c0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2016 License: MIT Imports: 4 Imported by: 0

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 https://pypi.python.org/pypi/hash_ring/ in Go with the extra methods to add and remove nodes.

Using

Importing ::

import "github.com/serialx/hashring"

Basic example usage ::

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")

Using weights example ::

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"] = 1

ring := hashring.NewWithWeights(weights)
server := ring.GetNode("my_key")

Adding and removing nodes example ::

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")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashKey

type HashKey uint32

type HashKeyOrder

type HashKeyOrder []HashKey

func (HashKeyOrder) Len

func (h HashKeyOrder) Len() int

func (HashKeyOrder) Less

func (h HashKeyOrder) Less(i, j int) bool

func (HashKeyOrder) Swap

func (h HashKeyOrder) Swap(i, j int)

type HashRing

type HashRing struct {
	// contains filtered or unexported fields
}

func New

func New(nodes []string) *HashRing

func NewWithWeights

func NewWithWeights(weights map[string]int) *HashRing

func (*HashRing) AddNode

func (h *HashRing) AddNode(node string) *HashRing

func (*HashRing) AddWeightedNode

func (h *HashRing) AddWeightedNode(node string, weight int) *HashRing

func (*HashRing) GenKey

func (h *HashRing) GenKey(key string) HashKey

func (*HashRing) GetNode

func (h *HashRing) GetNode(stringKey string) (node string, ok bool)

func (*HashRing) GetNodePos

func (h *HashRing) GetNodePos(stringKey string) (pos int, ok bool)

func (*HashRing) GetNodes

func (h *HashRing) GetNodes(stringKey string, size int) (nodes []string, ok bool)

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(node string) *HashRing

func (*HashRing) UpdateWeightedNode

func (h *HashRing) UpdateWeightedNode(node string, weight int) *HashRing

func (*HashRing) UpdateWithWeights

func (h *HashRing) UpdateWithWeights(weights map[string]int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL