consistenthash

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TopWeight is the top weight that one entry might set.
	TopWeight = 100
)

Variables

This section is empty.

Functions

func Hash

func Hash(data []byte) uint64

Hash returns the hash value of data.

Types

type ConsistentHash

type ConsistentHash interface {
	Add(node any)
	AddWithReplicas(node any, replicas int)
	AddWithWeight(node any, weight int)
	Get(v any) (any, bool)
	GetAllNodes() []any
	Remove(node any)
	GetNodesCount() int
}

type Func

type Func func(data []byte) uint64

Func defines the hash method.

type HashRing

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

A HashRing is implementation of consistent hash.

func NewCustomHashRing

func NewCustomHashRing(replicas int, fn Func) *HashRing

NewCustomHashRing returns a HashRing with given replicas and hash func.

func NewHashRing

func NewHashRing() *HashRing

NewHashRing returns a HashRing.

func (*HashRing) Add

func (h *HashRing) Add(node any)

Add adds the node with the number of h.replicas, the later call will overwrite the replicas of the former calls.

func (*HashRing) AddWithReplicas

func (h *HashRing) AddWithReplicas(node any, replicas int)

AddWithReplicas adds the node with the number of replicas, replicas will be truncated to h.replicas if it's larger than h.replicas, the later call will overwrite the replicas of the former calls.

func (*HashRing) AddWithWeight

func (h *HashRing) AddWithWeight(node any, weight int)

AddWithWeight adds the node with weight, the weight can be 1 to 100, indicates the percent, the later call will overwrite the replicas of the former calls.

func (*HashRing) Get

func (h *HashRing) Get(v any) (any, bool)

Get returns the corresponding node from h base on the given v.

func (*HashRing) GetAllNodes

func (h *HashRing) GetAllNodes() []any

GetAllNodes returns all nodes used in hash ring

return a slice with a string representation of the nodes

func (*HashRing) GetNodesCount

func (h *HashRing) GetNodesCount() int

GetNodesCount returns the current number of nodes

func (*HashRing) Remove

func (h *HashRing) Remove(node any)

Remove removes the given node from h.

Jump to

Keyboard shortcuts

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