Documentation ¶
Overview ¶
Package hashring implements a thread-safe consistent hashring with a pluggable hashing algorithm.
This package was developed for use in a gRPC balancer, but nothing precludes it from being used for any other purpose.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMemberAlreadyExists = errors.New("member node already exists") ErrMemberNotFound = errors.New("member node not found") ErrNotEnoughMembers = errors.New("not enough member nodes to satisfy request") ErrInvalidReplicationFactor = errors.New("replication factor must be at least 1") ErrVnodeNotFound = errors.New("vnode not found") ErrUnexpectedVnodeCount = errors.New("found a different number of vnodes than replication factor") )
Functions ¶
This section is empty.
Types ¶
type HashFunc ¶
HashFunc is the signature for any hashing function that can be leveraged by the hashring.
type Member ¶
type Member interface {
Key() string
}
Member represents a participating member of the hashring. In most use cases, you can think of a member as a node or backend.
type Ring ¶
Ring provides a thread-safe consistent hashring implementation with a configurable number of virtual nodes.
func MustNew ¶
MustNew creates a new Hashring with the specified hasher function and replication factor.
If the provided replication factor is less than 1, this function will panic.
func New ¶
New allocates a Ring with the specified hash function and replication factor.
The replication factor must be greater than 0 and ideally be at least 20 or higher for quality key distribution. At 100, the standard distribution of key->member mapping will be about 10% of the mean. At 1000, it will be about 3.2%. This value should be chosen very carefully because a higher value will require more memory and decrease member selection performance.
func (*Ring) Add ¶
Add inserts a member into the hashring.
If a member with the same key is already in the hashring, ErrMemberAlreadyExists is returned.
func (*Ring) FindN ¶
FindN finds the first N members after the specified key.
If there are not enough members to satisfy the request, ErrNotEnoughMembers is returned.