Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrIsEmpty = errors.New("consistenthash: is empty")
Functions ¶
This section is empty.
Types ¶
type ConsistentHash ¶
type ConsistentHash interface {
Add(nodes ...string)
Del(nodes ...string)
Get(key string) (node string, err error)
// @return: 返回的 map 的
// key 为添加到内部的 node,
// value 为该 node 在环上所占的 point 个数。
// 我们可以通过各个 node 对应的 point 个数是否接近,来判断各 node 在环上的分布是否均衡。
// map 的所有 value 加起来应该等于 (math.MaxUint32 + 1)
Nodes() map[string]uint64
}
func New ¶
func New(dups int, modOptions ...ModOption) ConsistentHash
@param dups: 每个实际的 node 转变成多少个环上的节点,必须大于等于1 @param modOptions: 可修改内部的哈希函数,比如替换成murmur32的开源实现,可以这样:
import "github.com/spaolacci/murmur3"
import "github.com/ericxiao417/ericool/consistenthash"
ch := consistenthash.New(1000, func(option *Option) {
option.hfn = func(bytes []byte) uint32 {
h := murmur3.New32()
h.Write(bytes)
return h.Sum32()
}
})
Click to show internal directories.
Click to hide internal directories.