Documentation
¶
Index ¶
- Constants
- Variables
- type Consistent
- func (c *Consistent) Add(nodeID string) error
- func (c *Consistent) AddWithReplicas(nodeID string, replicas int) error
- func (c *Consistent) Get(key string) (string, error)
- func (c *Consistent) GetN(key string, n int) ([]string, error)
- func (c *Consistent) IsEmpty() bool
- func (c *Consistent) Nodes() []string
- func (c *Consistent) Remove(nodeID string) error
- func (c *Consistent) SetHashFunc(hashFunc func([]byte) uint32) *Consistent
- func (c *Consistent) SetReplicas(replicas int) *Consistent
Constants ¶
const ( // DefaultReplicas is the default number of virtual nodes per physical node. // This value is based on Kong Gateway's configuration for achieving <1% distribution bias. DefaultReplicas = 160 )
Variables ¶
var ( // ErrEmptyRing is returned when trying to get a node from an empty ring. ErrEmptyRing = errors.New("consistent: hash ring is empty") // ErrNodeNotFound is returned when trying to remove a node that doesn't exist. ErrNodeNotFound = errors.New("consistent: node not found") // ErrNodeExists is returned when trying to add a node that already exists. ErrNodeExists = errors.New("consistent: node already exists") )
Functions ¶
This section is empty.
Types ¶
type Consistent ¶
type Consistent struct {
// contains filtered or unexported fields
}
Consistent represents a consistent hashing ring with virtual nodes. It provides a thread-safe implementation of consistent hashing algorithm.
func New ¶
func New() *Consistent
New creates a new Consistent hash ring with default configuration: - 160 virtual nodes per physical node - CRC32 hash function.
func (*Consistent) Add ¶
func (c *Consistent) Add(nodeID string) error
Add adds a physical node to the hash ring using the default replica count. It creates virtual nodes for the physical node and distributes them on the ring. Returns ErrNodeExists if the node already exists.
func (*Consistent) AddWithReplicas ¶
func (c *Consistent) AddWithReplicas(nodeID string, replicas int) error
AddWithReplicas adds or updates a physical node in the hash ring with a specific replica count. If the node already exists, it will be removed and re-added with the new replica count. This allows for weight-based consistent hashing by giving more virtual nodes to certain physical nodes.
func (*Consistent) Get ¶
func (c *Consistent) Get(key string) (string, error)
Get returns the physical node for the given key. It uses consistent hashing to find the closest node on the ring. Returns ErrEmptyRing if the ring is empty.
func (*Consistent) GetN ¶
func (c *Consistent) GetN(key string, n int) ([]string, error)
GetN returns the top N unique physical nodes in clockwise order starting from the hash of the key. This is used for consistent failover - if the first node is unavailable, the next nodes on the ring are used. Returns ErrEmptyRing if the ring is empty.
func (*Consistent) IsEmpty ¶
func (c *Consistent) IsEmpty() bool
IsEmpty returns true if the ring has no nodes.
func (*Consistent) Nodes ¶
func (c *Consistent) Nodes() []string
Nodes returns a list of all physical nodes in the ring.
func (*Consistent) Remove ¶
func (c *Consistent) Remove(nodeID string) error
Remove removes a physical node from the hash ring. It removes all virtual nodes associated with the physical node. Returns ErrNodeNotFound if the node doesn't exist.
func (*Consistent) SetHashFunc ¶
func (c *Consistent) SetHashFunc(hashFunc func([]byte) uint32) *Consistent
SetHashFunc sets a custom hash function. This method can be chained with other configuration methods. Returns the Consistent instance for method chaining.
func (*Consistent) SetReplicas ¶
func (c *Consistent) SetReplicas(replicas int) *Consistent
SetReplicas sets the number of virtual nodes per physical node. This method can be chained with other configuration methods. Returns the Consistent instance for method chaining.