consistenthash

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTokensPerNode = 100

DefaultTokensPerNode is the default number of virtual nodes per node.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hash

type Hash func(data string) uint64

Hash is a function computing a 64-bit digest.

var DefaultHash Hash = xxhash.Sum64String

DefaultHash is the default Hash used by Ring.

type Ring

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

Ring implements consistent hashing, aka. ring hash (not thread-safe). It hashes nodes and keys (strings) onto a ring of tokens. Keys are mapped to the next token (node) on the ring. Nodes cannot be removed. Instantiate a new Ring instead.

func New

func New(hash Hash, tokensPerNode int, initialNodes ...string) *Ring

New creates a new hash ring with the given configuration and adds the given nodes. The given Hash (or DefaultHash if nil) is used to hash nodes and keys (strings). Each node is assigned tokensPerNode tokens (or DefaultTokensPerNode if <= 0) – aka. virtual nodes – for a more uniform key distribution.

func (*Ring) AddNodes

func (r *Ring) AddNodes(nodes ...string)

AddNodes adds hash tokens for the given nodes to this Ring.

func (*Ring) Hash

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

Hash hashes the given key onto the ring of tokens and returns the node that belongs to the next token on the ring.

func (*Ring) IsEmpty

func (r *Ring) IsEmpty() bool

IsEmpty returns true if there are no nodes in this Ring.

Jump to

Keyboard shortcuts

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