consistenthash

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

本一致性哈希库支持自定义虚拟节点副本数,支持自定义哈希函数,支持添加删除节点或重置节点。 使用说明: 1、使用之前你需要用New函数创建一个hashring,参数有虚拟节点副本数和哈希函数 2、添加hash环上的node,如果有动态更新的话,可以添加和删除 3、通过Get方法获取一个数据对应到hash环上的哪个节点处理 样例代码可以参考test里面的代码。

The provided consistent hashing library supports customizing the number of virtual node replicas, defining custom hash functions, and adding, deleting, or resetting nodes. Usage instructions:

  1. Before using, create a hashring with the New function. The parameters for this function include the number of virtual node replicas and the desired hash function.
  2. Add nodes to the hash ring. If there are dynamic updates, nodes can be added and removed.
  3. Use the Get method to determine which node on the hash ring is responsible for processing a specific piece of data. Refer to the sample code in the tests for more details.

Documentation

Overview

Package consistenthash provides a flexible hash ring. 支持自定义虚拟节点副本数,支持自定义哈希函数,支持添加删除节点的一致性hash库。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRingEmpty "ring empty"
	ErrRingEmpty = errors.New("ring empty")
	// ErrRingFull "ring full"
	ErrRingFull = errors.New("ring full")
)

Functions

This section is empty.

Types

type Hash32

type Hash32 func(data []byte) uint32

Hash32 func with uint32 return 输出为uint32的hash函数

type HashRing

type HashRing struct {
	HashFunc Hash32 // hash func for key and for node

	sync.RWMutex
	// contains filtered or unexported fields
}

HashRing core struct for hashring 一致性hash环的结构体

func New

func New(replicas uint16, hash Hash32) *HashRing

New creates a new hash ring. With default hash function crc32. 创建hash环,默认hash函数为crc32.ChecksumIEEE

func (*HashRing) Add

func (m *HashRing) Add(nodes ...string) error

Add adds some nodes to the hashring. If return error,MUST ResetAll hashring, typically by adjusting the replicas! 返回错误,必须接收和处理

func (*HashRing) Get

func (m *HashRing) Get(key string) (string, error)

Get gets the closest node in the hashring to the provided key. 获取key对应的节点

func (*HashRing) IsEmpty

func (m *HashRing) IsEmpty() bool

IsEmpty 是否空

func (*HashRing) Remove

func (m *HashRing) Remove(nodes ...string)

Remove removes some nodes from the hash. 删除节点

func (*HashRing) Reset

func (m *HashRing) Reset(nodes ...string) error

Reset reset nodes If return error,MUST ResetAll hashring, typically by adjusting the replicas! 返回错误,必须接收和处理

func (*HashRing) ResetAll

func (m *HashRing) ResetAll(replicas uint16, hash Hash32, nodes ...string) error

ResetAll 重置

func (*HashRing) RingInfo

func (m *HashRing) RingInfo() string

RingInfo information about the hash ring.

type U32Slice

type U32Slice []uint32

U32Slice implement for sorting 实现sort.Interface接口的Uint32Slice

func (U32Slice) Len

func (x U32Slice) Len() int

Len returns the length of the uints array.

func (U32Slice) Less

func (x U32Slice) Less(i, j int) bool

Less returns true if element i is less than element j.

func (U32Slice) Swap

func (x U32Slice) Swap(i, j int)

Swap exchanges elements i and j.

Jump to

Keyboard shortcuts

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