consistenthash

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package consistenthash provides an implementation of a ring hash.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FNVHash added in v1.4.0

func FNVHash(data []byte) uint32

FNVHash is an alternate hash function for Map, and wraps/adapts fnv.New32a. It is generally a better choice of hash function than the default crc32 with the IEEE polynomial (crc32.ChecksumIEEE). (the default cannot be updated without breaking existing systems)

Types

type Hash

type Hash func(data []byte) uint32

Hash maps the data to a uint32 hash-ring

type Map

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

Map tracks segments in a hash-ring, mapped to specific keys.

func New

func New(segsPerKey int, fn Hash) *Map

New constructs a new consistenthash hashring, with segsPerKey segments per added key. It is recommended to use FNVHash as the second (fn) argument for new applications/systems.

func (*Map) Add

func (m *Map) Add(ownerKeys ...string)

Add adds some keys to the hashring, establishing ownership of segsPerKey segments.

func (*Map) Get

func (m *Map) Get(key string) string

Get gets the closest item in the hash to the provided key.

func (*Map) GetReplicated

func (m *Map) GetReplicated(key string, keyReplicas int) []string

GetReplicated gets the closest item in the hash to a deterministic set of keyReplicas variations of the provided key. The returned set of segment-owning keys is dedup'd, and collisions are resolved by traversing backwards in the hash-ring to find an unused owning-key.

func (*Map) IsEmpty

func (m *Map) IsEmpty() bool

IsEmpty returns true if there are no items available.

func (*Map) OwnerRangeSummary added in v1.2.0

func (m *Map) OwnerRangeSummary(ownerKey string, skipOverrideKeys int) (RangeSummary, bool)

OwnerRangeSummary provides an object that provides an efficient check as to whether a key is owned by a specific owner-key. (where owner-keys would be provided to [(*Map).Add] and a `key` would be passed to [(*Map).Get]).

skipOverrideKeys allows one to provide a broader bound on ownership that includes the removal of `skipOverrideKeys` owners.

Returns nil if the key is not present in this map.

type RangeSummary added in v1.2.0

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

func (*RangeSummary) OwnerPossiblyOwns added in v1.2.0

func (r *RangeSummary) OwnerPossiblyOwns(key string, replicas int) bool

Directories

Path Synopsis
Package chtest provides some helper hash-functions for use with the parent consistenthash package (and galaxycache) to provide particular owners for specific keys.
Package chtest provides some helper hash-functions for use with the parent consistenthash package (and galaxycache) to provide particular owners for specific keys.

Jump to

Keyboard shortcuts

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