hashring

package
v0.3.12 Latest Latest
Warning

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

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

README

hashring

A golang consistent hashring

Install

go get github.com/cocktail828/go-tools/hashring

Usage

get more details from hashring_test.go

// virtualSpots means virtual spots created by each node
nodeWeight := make(map[string]int)
nodeWeight["node1"] = 1
nodeWeight["node2"] = 1
nodeWeight["node3"] = 2
vitualSpots := 100
hash := NewHashRing(virtualSpots)
	
	
//add nodes
hash.AddNodes(nodeWeight)
	
//remove node
hash.RemoveNode("node3")

	
//add node
hash.AddNode("node3", 3)

	
//get key's node
node := hash.GetNode("key")

Documentation

Index

Constants

View Source
const (
	//DefaultVirualSpots default virual spots
	DefaultVirualSpots = 300
)

Variables

This section is empty.

Functions

func Crc32 added in v0.3.12

func Crc32(nodeKey string) uint32

func Md5 added in v0.3.12

func Md5(nodeKey string) uint32

func Sha256 added in v0.3.12

func Sha256(nodeKey string) uint32

Types

type HashFunc added in v0.3.12

type HashFunc func(string) uint32

type HashRing

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

HashRing store nodes and weigths

func New

func New(opts ...Option) *HashRing

NewHashRing create a hash ring with virual spots default Hash crc32

func (*HashRing) AddNode

func (h *HashRing) AddNode(nodeKey string, weight int)

AddNode add node to hash ring

func (*HashRing) AddNodes

func (h *HashRing) AddNodes(nodeWeight map[string]int)

AddNodes add nodes to hash ring

func (*HashRing) GetNode

func (h *HashRing) GetNode(s string) string

GetNode get node with key

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeKey string)

RemoveNode remove node

func (*HashRing) UpdateNode

func (h *HashRing) UpdateNode(nodeKey string, weight int)

UpdateNode update node with weight

type Option added in v0.3.12

type Option func(*HashRing)

func WithHash added in v0.3.12

func WithHash(f HashFunc) Option

set hash func

func WithSpots added in v0.3.12

func WithSpots(n int) Option

set num of virtual nodes

Jump to

Keyboard shortcuts

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