ringhash

package
v0.7.0 Latest Latest
Warning

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

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

Documentation

Overview

Package ringhash provides a ring hash implementation.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoNode when there is no node added into the hash ring.
	ErrNoNode = errors.New("no node added")
	// ErrNodeNotFound when no node found in LoadMap.
	ErrNodeNotFound = errors.New("node not found in LoadMap")
	// DefaultConfig is the default config for hash ring.
	DefaultConfig = Config{
		HashFn:          hash,
		Replicas:        10,
		BalancingFactor: 1.25,
	}
)

Functions

This section is empty.

Types

type Config

type Config struct {
	HashFn          Hash
	Replicas        int
	BalancingFactor float64
}

Config is the config for hash ring.

type Hash

type Hash func(key string) uint64

Hash is the hash function.

type Node

type Node struct {
	Name string
	Load int64
}

Node is the node in the ring.

type Option

type Option = func(*Config) error

Option applies config to Config.

type Ring

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

Ring is the data store for keys hash map.

func New

func New(options ...Option) *Ring

New returns a new Ring.

func (*Ring) Add

func (r *Ring) Add(node string) bool

Add increases load of the given node by 1, should only be used with GetLeast.

func (*Ring) AddNode

func (r *Ring) AddNode(keys ...string)

AddNode adds Node with key as name to the hash ring.

func (*Ring) Done

func (r *Ring) Done(node string) bool

Done decreases load of the given node by 1, should only be used with GetLeast.

func (*Ring) GetLeastNode

func (r *Ring) GetLeastNode(key string) (string, error)

GetLeastNode uses consistent hashing with bounded loads to get the least loaded node.

func (*Ring) GetNode

func (r *Ring) GetNode(key string) (string, error)

GetNode returns the closest node in the hash ring to the provided key.

func (*Ring) IsEmpty

func (r *Ring) IsEmpty() bool

IsEmpty returns true if there is no node in the ring.

func (*Ring) Loads

func (r *Ring) Loads() map[string]int64

Loads returns the loads of all the nodes in the hash ring.

func (*Ring) MaxLoad

func (r *Ring) MaxLoad() int64

MaxLoad returns the maximum load for a single node in the hash ring, which is (totalLoad/numberOfNodes)*balancingFactor.

func (*Ring) Nodes

func (r *Ring) Nodes() (nodes []string)

Nodes returns the list of nodes in the hash ring.

func (*Ring) RemoveNode

func (r *Ring) RemoveNode(node string) bool

RemoveNode deletes node from the hash ring.

func (*Ring) UpdateLoad

func (r *Ring) UpdateLoad(node string, load int64)

UpdateLoad sets load of the given node to the given load.

Jump to

Keyboard shortcuts

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