Documentation ¶
Overview ¶
Package hrw implements Rendezvous hashing. http://en.wikipedia.org/wiki/Rendezvous_hashing.
Example ¶
// given a set of servers servers := []string{ "one.example.com", "two.example.com", "three.example.com", "four.example.com", "five.example.com", "six.example.com", } // HRW can consistently select a uniformly-distributed set of servers for // any given key var ( key = []byte("/examples/object-key") h = Hash(key) ) SortSliceByValue(servers, h) for id := range servers { fmt.Printf("trying GET %s%s\n", servers[id], 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 ¶
- Constants
- func Hash(key []byte) uint64
- func Sort(nodes []uint64, hash uint64) []uint64
- func SortByWeight(nodes []uint64, weights []float64, hash uint64) []uint64
- func SortHasherSliceByValue[T Hasher](slice []T, hash uint64)
- func SortHasherSliceByWeightValue[T Hasher](slice []T, weights []float64, hash uint64)
- func SortSliceByIndex(slice interface{}, hash uint64)
- func SortSliceByValue(slice interface{}, hash uint64)
- func SortSliceByWeightIndex(slice interface{}, weights []float64, hash uint64)
- func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64)
- func StringHash(key string) uint64
- func ValidateWeights(weights []float64) error
- type Hasher
Examples ¶
Constants ¶
const ( NormalizedMaxWeight = 1.0 NormalizedMinWeight = 0.0 )
Boundaries of valid normalized weights
Variables ¶
This section is empty.
Functions ¶
func SortByWeight ¶
SortByWeight receive nodes, weights and hash, and sort it by distance * weight
func SortHasherSliceByValue ¶
SortHasherSliceByValue receives []Hasher and hash to sort by value-distance.
func SortHasherSliceByWeightValue ¶
SortHasherSliceByWeightValue receives []Hasher, weights and hash to sort by value-distance * weights.
func SortSliceByIndex ¶
func SortSliceByIndex(slice interface{}, hash uint64)
SortSliceByIndex received []T and hash to sort by index-distance
func SortSliceByValue ¶
func SortSliceByValue(slice interface{}, hash uint64)
SortSliceByValue received []T and hash to sort by value-distance
func SortSliceByWeightIndex ¶
SortSliceByWeightIndex received []T, weights and hash to sort by index-distance * weights
func SortSliceByWeightValue ¶
SortSliceByWeightValue received []T, weights and hash to sort by value-distance * weights
func StringHash ¶ added in v1.2.1
StringHash uses murmur3 hash to return uint64
func ValidateWeights ¶
ValidateWeights checks if weights are normalized between 0.0 and 1.0