Documentation ¶
Overview ¶
An implementation of Consistent Hashing and Consistent Hashing With Bounded Loads.
https://en.wikipedia.org/wiki/Consistent_hashing
https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
Example (Bounded) ¶
package main import ( "log" "testing" "github.com/lafikl/consistent" ) func main(t *testing.T) { c := consistent.New() // adds the hosts to the ring c.Add("127.0.0.1:8000") c.Add("92.0.0.1:8000") // Returns the host that owns `key`. // // As described in https://en.wikipedia.org/wiki/Consistent_hashing // // It returns ErrNoHosts if the ring has no hosts in it. host, err := c.Get("/app.html") if err != nil { log.Fatal(err) } log.Println(host) } func main() { c := consistent.New() // adds the hosts to the ring c.Add("127.0.0.1:8000") c.Add("92.0.0.1:8000") // It uses Consistent Hashing With Bounded loads // https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html // to pick the least loaded host that can serve the key // // It returns ErrNoHosts if the ring has no hosts in it. // host, err := c.GetLeast("/app.html") if err != nil { log.Fatal(err) } // increases the load of `host`, we have to call it before sending the request c.Inc(host) // send request or do whatever log.Println("send request to", host) // call it when the work is done, to update the load of `host`. c.Done(host) }
Output:
Index ¶
- Variables
- type Consistent
- func (c *Consistent) Add(host string)
- func (c *Consistent) Done(host string)
- func (c *Consistent) Get(key string) (string, error)
- func (c *Consistent) GetLeast(key string) (string, error)
- func (c *Consistent) GetLoads() map[string]int64
- func (c *Consistent) Hosts() (hosts []string)
- func (c *Consistent) Inc(host string)
- func (c *Consistent) MaxLoad() int64
- func (c *Consistent) Remove(host string) bool
- func (c *Consistent) UpdateLoad(host string, load int64)
- type Host
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoHosts = errors.New("no hosts added")
Functions ¶
This section is empty.
Types ¶
type Consistent ¶
func New ¶
func New() *Consistent
func (*Consistent) Add ¶
func (c *Consistent) Add(host string)
func (*Consistent) Done ¶
func (c *Consistent) Done(host string)
Decrements the load of host by 1
should only be used with if you obtained a host with GetLeast
func (*Consistent) Get ¶
func (c *Consistent) Get(key string) (string, error)
Returns the host that owns `key`.
As described in https://en.wikipedia.org/wiki/Consistent_hashing
It returns ErrNoHosts if the ring has no hosts in it.
func (*Consistent) GetLeast ¶
func (c *Consistent) GetLeast(key string) (string, error)
It uses Consistent Hashing With Bounded loads
https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html
to pick the least loaded host that can serve the key
It returns ErrNoHosts if the ring has no hosts in it.
func (*Consistent) GetLoads ¶
func (c *Consistent) GetLoads() map[string]int64
Returns the loads of all the hosts
func (*Consistent) Hosts ¶
func (c *Consistent) Hosts() (hosts []string)
Return the list of hosts in the ring
func (*Consistent) Inc ¶
func (c *Consistent) Inc(host string)
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
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) Remove ¶
func (c *Consistent) Remove(host string) bool
Deletes host from the ring
func (*Consistent) UpdateLoad ¶
func (c *Consistent) UpdateLoad(host string, load int64)
Sets the load of `host` to the given `load`