consistenthash

package
v0.17.1-0...-36a5d2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIsEmpty = errors.New("naza.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/souliot/naza/pkg/consistenthash"

ch := consistenthash.New(1000, func(option *Option) {
  option.hfn = func(bytes []byte) uint32 {
    h := murmur3.New32()
    h.Write(bytes)
    return h.Sum32()
  }
})

type HashFunc

type HashFunc func([]byte) uint32

type ModOption

type ModOption func(option *Option)

type Option

type Option struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL