Documentation ¶
Overview ¶
Package consistent provides a consistent hashing function.
Consistent hashing is often used to distribute requests to a changing set of servers. For example, say you have some cache servers cacheA, cacheB, and cacheC. You want to decide which cache server to use to look up information on a user.
You could use a typical hash table and hash the user id to one of cacheA, cacheB, or cacheC. But with a typical hash table, if you add or remove a server, almost all keys will get remapped to different results, which basically could bring your service to a grinding halt while the caches get rebuilt.
With a consistent hash, adding or removing a server drastically reduces the number of keys that get remapped.
Read more about consistent hashing on wikipedia: http://en.wikipedia.org/wiki/Consistent_hashing
Index ¶
- Variables
- type Config
- type Consistent
- func (c *Consistent) Add(elt string, numbersOfReplicas ...int)
- func (c *Consistent) Get(name string) (string, error)
- func (c *Consistent) GetN(name string, n int) ([]string, error)
- func (c *Consistent) GetTwo(name string) (string, string, error)
- func (c *Consistent) MemberReplicas() map[string]int
- func (c *Consistent) Members() []string
- func (c *Consistent) Remove(elt string) bool
- func (c *Consistent) Set(elts []string)
- func (c *Consistent) SetWithReplicas(elts []SetElt)
- type Hasher
- type SetElt
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyCircle = errors.New("empty circle")
ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.
Functions ¶
This section is empty.
Types ¶
type Consistent ¶
Consistent holds the information about the members of the consistent hash circle.
func New ¶
func New(conf Config) *Consistent
New creates a new Consistent object with a default setting of 20 replicas for each entry.
To change the number of replicas, set NumberOfReplicas before adding entries.
func (*Consistent) Add ¶
func (c *Consistent) Add(elt string, numbersOfReplicas ...int)
Add inserts a string element in the consistent hash.
func (*Consistent) Get ¶
func (c *Consistent) Get(name string) (string, error)
Get returns an element close to where name hashes to in the circle.
func (*Consistent) GetN ¶
func (c *Consistent) GetN(name string, n int) ([]string, error)
GetN returns the N closest distinct elements to the name input in the circle.
func (*Consistent) GetTwo ¶
func (c *Consistent) GetTwo(name string) (string, string, error)
GetTwo returns the two closest distinct elements to the name input in the circle.
func (*Consistent) MemberReplicas ¶ added in v1.0.8
func (c *Consistent) MemberReplicas() map[string]int
func (*Consistent) Members ¶
func (c *Consistent) Members() []string
func (*Consistent) Remove ¶
func (c *Consistent) Remove(elt string) bool
Remove removes an element from the hash. return true for Remove success, false for Remove does not work
func (*Consistent) Set ¶
func (c *Consistent) Set(elts []string)
Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed. defaultNumberOfReplicas will be used to add member
func (*Consistent) SetWithReplicas ¶ added in v1.0.5
func (c *Consistent) SetWithReplicas(elts []SetElt)
SetWithReplicas sets all the elements in the hash with NumberOfReplicas. If there are existing elements not present in elts, they will be removed.