golang 八行代码实现的一致性哈希

By at • 2093次点击
youbbs

A Fast, Minimal Memory, Consistent Hash Algorithm https://arxiv.org/abs/1406.2294 14

https://github.com/dgryski/go-jump 24

// Hash consistently chooses a hash bucket number in the range [0, numBuckets) for the given key. numBuckets must be >= 1.
func Hash(key uint64, numBuckets int) int32 {

	var b int64 = -1
	var j int64

	for j < int64(numBuckets) {
		b = j
		key = key*2862933555777941757 + 1
		j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
	}

	return int32(b)
}

另外一个扩展 https://github.com/renstrom/go-jump-consistent-hash 53


哈希, golang, 八行, 一致性, 代码


目前尚无回复
登录 后发表评论