Documentation ¶
Overview ¶
Package core
An implementation of Consistent Hashing Algorithm described in Golang ¶
Index ¶
- Variables
- type Consistent
- func (c *Consistent) Done(host string)
- func (c *Consistent) GetKey(key string) (string, error)
- func (c *Consistent) GetKeyLeast(key string) (string, error)
- func (c *Consistent) GetLoads() map[string]int64
- func (c *Consistent) Hosts() []string
- func (c *Consistent) Inc(hostName string)
- func (c *Consistent) MaxLoad() int64
- func (c *Consistent) RegisterHost(hostName string) error
- func (c *Consistent) UnregisterHost(hostName string) error
- func (c *Consistent) UpdateLoad(host string, load int64)
- type Host
Constants ¶
This section is empty.
Variables ¶
var ( ErrHostAlreadyExists = errors.New("host already exists") ErrHostNotFound = errors.New("host not found") )
Functions ¶
This section is empty.
Types ¶
type Consistent ¶
type Consistent struct { // the hash ring lock sync.RWMutex // contains filtered or unexported fields }
Consistent is an implementation of consistent-hashing-algorithm
func NewConsistent ¶
func NewConsistent(replicaNum int, hashFunc func(key string) uint64) *Consistent
func (*Consistent) Done ¶
func (c *Consistent) Done(host string)
Done Decrements the load of host by 1
should only be used with if you obtained a host with GetLeast
func (*Consistent) GetKeyLeast ¶
func (c *Consistent) GetKeyLeast(key string) (string, error)
GetKeyLeast It uses consistent-hashing With Bounded loads to pick the least loaded host that can serve the key
It returns ErrNoHosts if the ring has no hosts in it.
ref: https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
func (*Consistent) GetLoads ¶
func (c *Consistent) GetLoads() map[string]int64
GetLoads Returns the loads of all the hosts
func (*Consistent) Hosts ¶
func (c *Consistent) Hosts() []string
Hosts Return the list of real hosts
func (*Consistent) Inc ¶
func (c *Consistent) Inc(hostName string)
Inc Increments the load of host by 1
should only be used with if you obtained a host with GetLeast
func (*Consistent) MaxLoad ¶
func (c *Consistent) MaxLoad() int64
MaxLoad Returns the maximum load of the single host which is: (total_load/number_of_hosts)*1.25 total_load is the total number of active requests served by hosts for more info:
https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
func (*Consistent) RegisterHost ¶
func (c *Consistent) RegisterHost(hostName string) error
func (*Consistent) UnregisterHost ¶
func (c *Consistent) UnregisterHost(hostName string) error
func (*Consistent) UpdateLoad ¶
func (c *Consistent) UpdateLoad(host string, load int64)
UpdateLoad Sets the load of `host` to the given `load`