import "github.com/andy2046/gopie/pkg/ringhash"
Package ringhash provides a ring hash implementation.
- Variables
- type Config
- type Hash
- type Node
- type Option
- type Ring
- func New(options ...Option) *Ring
- func (r *Ring) Add(node string) bool
- func (r *Ring) AddNode(keys ...string)
- func (r *Ring) Done(node string) bool
- func (r *Ring) GetLeastNode(key string) (string, error)
- func (r *Ring) GetNode(key string) (string, error)
- func (r *Ring) IsEmpty() bool
- func (r *Ring) Loads() map[string]int64
- func (r *Ring) MaxLoad() int64
- func (r *Ring) Nodes() (nodes []string)
- func (r *Ring) RemoveNode(node string) bool
- func (r *Ring) UpdateLoad(node string, load int64)
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 (r *Ring) Add(node string) bool
Add increases load of the given node by 1, should only be used with GetLeast.
func (r *Ring) AddNode(keys ...string)
AddNode adds Node with key as name to the hash ring.
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 (r *Ring) GetNode(key string) (string, error)
GetNode returns the closest node in the hash ring to the provided key.
func (r *Ring) IsEmpty() bool
IsEmpty returns true if there is no node in the ring.
func (r *Ring) Loads() map[string]int64
Loads returns the loads of all the nodes in the hash ring.
func (r *Ring) MaxLoad() int64
MaxLoad returns the maximum load for a single node in the hash ring, which is (totalLoad/numberOfNodes)*balancingFactor.
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.
Generated by godoc2md