hashring

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: Apache-2.0 Imports: 5 Imported by: 1

README

hashring

Provides a simple implementation of a consistent hash ring. By default it uses the 32-bit cyclic redundancy check (CRC-32) for calculating checksums, but can take any function that takes a byte slice and returns an unsigned 32bit integer. The current implementation, is not as efficient as it could be and can also be prone to hotspots though it should consistently distribute across the nodes +/- 3 percent (dependent on the number of replicates).

Install

go get stvz.io/hashring

Usage

import "stvz.io/hashring"

// Initialize the ring with 3 replicas, using the default checksum function and
// add some nodes to the ring.
ring := hashring.New(3, nil)
ring.Add("server1", "server2", "server3", "server4", "server5")

To get the node from the ring, use the Get(key string) function. If there are no nodes available, the method returns an empty string.

if node := ring.Get("id"); node != "" {
  sendFn(node, data)
}

For pull style distributed applications, the function Mine(name, key string) exists to determine whether or not the key that is being pulled belongs to the worker. It returns a boolean value. Assuming that the nodes use the hostname of the worker the following is an example:

hostname, _ := os.Hostname()
if ring.Mine(hostname, "id") {
  processFn(data)
}

To remove a node (or nodes) use the Remove(node string) function.

ring.Remove("server1", "server2")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashFn

type HashFn func([]byte) uint32

type Ring

type Ring struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewRing

func NewRing(replicas int, fn HashFn) *Ring

func (*Ring) Add

func (r *Ring) Add(keys ...string)

func (*Ring) Get

func (r *Ring) Get(key string) string

func (*Ring) IsEmpty

func (r *Ring) IsEmpty() bool

func (*Ring) Mine

func (r *Ring) Mine(name, key string) bool

func (*Ring) Remove

func (r *Ring) Remove(keys ...string)

Jump to

Keyboard shortcuts

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