Skip to content

Latest commit

 

History

History
258 lines (141 loc) · 5.19 KB

ringhash.md

File metadata and controls

258 lines (141 loc) · 5.19 KB

ringhash

import "github.com/andy2046/gopie/pkg/ringhash"

Package ringhash provides a ring hash implementation.

ringhash.go

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,
    }
)
type Config struct {
    HashFn          Hash
    Replicas        int
    BalancingFactor float64
}

Config is the config for hash ring.

type Hash func(key string) uint64

Hash is the hash function.

type Node struct {
    Name string
    Load int64
}

Node is the node in the ring.

type Option = func(*Config) error

Option applies config to Config.

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

Ring is the data store for keys hash map.

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 (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 (r *Ring) RemoveNode(node string) bool

RemoveNode deletes node from the hash ring.

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

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


Generated by godoc2md