Documentation ¶
Overview ¶
Package hrw implements Rendezvous hashing. https://en.wikipedia.org/wiki/Rendezvous_hashing.
Example ¶
// given a set of servers servers := []Hashable{ hashString("one.example.com"), hashString("two.example.com"), hashString("three.example.com"), hashString("four.example.com"), hashString("five.example.com"), hashString("six.example.com"), } // HRW can consistently select a uniformly-distributed set of servers for // any given key var key = []byte("/examples/object-key") Sort(servers, WrapBytes(key)) for id := range servers { fmt.Printf("trying GET %s%s\n", servers[id], string(key)) }
Output: trying GET three.example.com/examples/object-key trying GET two.example.com/examples/object-key trying GET five.example.com/examples/object-key trying GET six.example.com/examples/object-key trying GET one.example.com/examples/object-key trying GET four.example.com/examples/object-key
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Hash ¶
Hash hashes the data in a fashion similar to the Hashable interface. Can be used as a hash function required for the HRW hashing algorithm.
func Sort ¶
func Sort[V, P Hashable](vv []V, object P)
Sort defines and sorts the scores for the provided hashable entities against the provided hashable object (in its general sense). See Hashable, HashableBytes and https://en.wikipedia.org/wiki/Rendezvous_hashing.
func SortWeighted ¶
func SortWeighted[V, P Hashable, W constraints.Float](vv []V, weights []W, object P)
SortWeighted is the same as Sort but allows using weights for a slice being sorted. A weight is applied to the corresponding element's score in the resulting slice. A weight allows modifying the default (equal to any element) probability of an element to win HRW sorting. Value slice's length and weight slice's length MUST be the same. Weights MUST be in [0.0; 1.0] range.
Types ¶
type HashableBytes ¶
type HashableBytes []byte
HashableBytes implements Hashable interface over raw data. Use WrapBytes to instantiate a correct byte slice wrapper.
func WrapBytes ¶
func WrapBytes(b []byte) HashableBytes
WrapBytes creates HashableBytes that implements Hashable interface over a raw data. Can be used for Sort and SortWeighted.
func (HashableBytes) Hash ¶
func (h HashableBytes) Hash() uint64