hashring

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 3 Imported by: 0

README

consistent hashring

consistent hashring for golang(>=1.18)

Usage

go get -u github.com/lockp111/consistent-hashring
NewManager
func main(){
    type NodeInfo struct{
        Host string
        Name string
    }
    manager = NewManager[NodeInfo](100)
}
Add & Remove & SetWeight
func main(){
    nodeInfo := &NodeInfo{
        Host: "127.0.0.1"
        Name: "localhost"
    }

    node := NewNode(node.Name, nodeInfo)
    node.SetWeight(10)
    manager.Add(node)

    manager.Remove(nodeInfo.Name)
}
GetNode & GetNodes
func main(){
    node, ok := manager.GetNode("localhost")
    nodes := manager.GetNodes()
}
FindOne
func main(){
    slot, ok := manager.FindOne("xxxxx")
    nodeInfo := slot.GetValue()
}
FindNext & FindPrev
func main(){
    slots := manager.FindNext("xxxxx", 10) //or slots := manager.FindPrev("xxxxx", 10)
    for _, slot := range slots {
        nodeInfo := slot.GetValue()
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node[T any] struct {
	Data T
	// contains filtered or unexported fields
}

func NewNode

func NewNode[T any](key string, data T) *Node[T]

func (*Node[T]) GetKey added in v1.0.1

func (n *Node[T]) GetKey() string

GetKey

func (*Node[T]) GetWeight added in v1.0.1

func (n *Node[T]) GetWeight() int

GetWeight

func (*Node[T]) SetWeight

func (n *Node[T]) SetWeight(w int)

SetWeight

func (*Node[T]) VirtualKeys added in v1.0.2

func (n *Node[T]) VirtualKeys(replicas int) []string

VirtualKeys returns virtual keys

func (*Node[T]) Virtuals

func (n *Node[T]) Virtuals(replicas int) []hashring.Slot[T]

Virtuals returns virtual nodes

type NodeManager

type NodeManager[T any] struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager[T any](replicas int) *NodeManager[T]

func (*NodeManager[T]) Add

func (m *NodeManager[T]) Add(node *Node[T])

Add node to manager

func (*NodeManager[T]) Count

func (m *NodeManager[T]) Count() int

Count returns manager nodes count

func (*NodeManager[T]) FindNext

func (m *NodeManager[T]) FindNext(key string, num int) []hashring.Slot[T]

FindNext returns hashring slots by key and next num

func (*NodeManager[T]) FindOne

func (m *NodeManager[T]) FindOne(key string) (hashring.Slot[T], bool)

FindOne returns hashring slot by key

func (*NodeManager[T]) FindPrev

func (m *NodeManager[T]) FindPrev(key string, num int) []hashring.Slot[T]

FindPrev returns hashring slots by key and prev num

func (*NodeManager[T]) GetNode

func (m *NodeManager[T]) GetNode(key string) (*Node[T], bool)

GetNode returns manager node

func (*NodeManager[T]) GetNodes

func (m *NodeManager[T]) GetNodes() []Node[T]

GetNodes returns total manager nodes

func (*NodeManager[T]) Remove

func (m *NodeManager[T]) Remove(nodeKey string)

Remove node by key

func (*NodeManager[T]) Slots

func (m *NodeManager[T]) Slots() int

Slots returns hashring slots count

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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