hashring

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 5 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 = 400
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HashRing

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

HashRing store nodes and weigths

func New

func New(spots int) *HashRing

NewHashRing create a hash ring with virual spots

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

Jump to

Keyboard shortcuts

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