conhash

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 4 Imported by: 1

README

conhash

A simple implementation of Consistent Hashing in Go.

Usage

Creating a New Ring

nodes := []conhash.Node{
    {Host: "localhost", Port: 8000},
    {Host: "localhost", Port: 8001},
    {Host: "localhost", Port: 8002},
}

ring := conhash.New(nodes)

Find a Target Node

target := ring.Find("your-key")

Add a New Node

node := conhash.Node{Host: "localhost", Port: 8003}
err := ring.Add(node)
if err != nil { 
    // ...
}

Remove a Node

node := conhash.Node{Host: "localhost", Port: 8003}
err := ring.Remove(node)
if err != nil { 
    // ...
}

Contributing

All contributions are welcome, just open a pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAddExceedsRingSize = errors.New("cannot add a new node, new size will exceed maximum ring size")
	ErrNodeNotFound       = errors.New("node not found")
)

Errors relating to common ring issues when handling nodes.

Functions

This section is empty.

Types

type Node

type Node struct {
	Host string
	Port int
	// contains filtered or unexported fields
}

Node represents a host machine.

type Ring

type Ring struct {
	Size  uint64
	Nodes []Node
}

Ring represents a circular ring that stores the provided nodes. The ring size is set to len(nodes) << 3.

func New

func New(nodes []Node) Ring

New will take in a slice of nodes and placed them in ascending order on the network ring.

func (*Ring) Add

func (r *Ring) Add(n Node) error

Add will add a new node into the ring, if the new size would exceed the ring size the function will return an error rather than increase the size creating a full rehash requirement on all nodes.

func (Ring) Find

func (r Ring) Find(key string) Node

FindByHash searches through the ring to find the closest node for a given hash.

func (*Ring) Remove

func (r *Ring) Remove(node Node) error

Remove will remove an existing node from the ring, if the node cannot be found then an error will be returned.

Jump to

Keyboard shortcuts

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