hashring

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoNodes = errors.New("no nodes available in the hash ring")

ErrNoNodes is returned when attempting to get a node from an empty ring

View Source
var ErrNodeExists = errors.New("node already exists in the hash ring")

ErrNodeExists is returned when attempting to add a node that already exists

View Source
var ErrNodeNotFound = errors.New("node not found in the hash ring")

ErrNodeNotFound is returned when attempting to operate on a node that doesn't exist

Functions

This section is empty.

Types

type HashRing

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

HashRing implements a consistent hash ring

func NewHashRing

func NewHashRing(replicaCount int) *HashRing

NewHashRing creates a new consistent hash ring

func (*HashRing) AddNode

func (h *HashRing) AddNode(n *Node) error

AddNode adds a node to the hash ring

func (*HashRing) GetNode

func (h *HashRing) GetNode(key string) (*Node, error)

GetNode returns the node responsible for the given key

func (*HashRing) GetNodeCount

func (h *HashRing) GetNodeCount() int

GetNodeCount returns the number of nodes in the hash ring

func (*HashRing) GetNodes

func (h *HashRing) GetNodes() []*Node

GetNodes returns all nodes in the hash ring

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeID string) error

RemoveNode removes a node from the hash ring

func (*HashRing) UpdateNodeStatus

func (h *HashRing) UpdateNodeStatus(nodeID string, status NodeStatus) error

UpdateNodeStatus updates a node's status

type Node

type Node struct {
	// ID is the unique identifier for this node
	ID string

	// Address is the network address of the node
	Address string

	// Status indicates the current operational status
	Status NodeStatus

	// LastHeartbeat is the Unix timestamp of the last heartbeat received
	LastHeartbeat int64
}

Node represents a physical node in the system

func NewNode

func NewNode(id, address string) *Node

NewNode creates a new node with the given ID and address

func (*Node) IsAvailable

func (n *Node) IsAvailable() bool

IsAvailable returns true if the node is available to handle requests

func (*Node) SetStatus

func (n *Node) SetStatus(status NodeStatus)

SetStatus updates the node's status

func (*Node) UpdateHeartbeat

func (n *Node) UpdateHeartbeat(timestamp int64)

UpdateHeartbeat updates the last heartbeat timestamp

type NodeStatus

type NodeStatus string

Status represents the current state of a node

const (
	// StatusActive indicates the node is operational and available
	NodeStatusActive NodeStatus = "active"

	// StatusInactive indicates the node is not operational
	NodeStatusInactive NodeStatus = "inactive"

	// StatusDraining indicates the node is preparing to be removed
	NodeStatusDraining NodeStatus = "draining"
)

type Ring

type Ring interface {
	// AddNode adds a new node to the hash ring
	AddNode(node *Node) error

	// RemoveNode removes a node from the hash ring
	RemoveNode(nodeID string) error

	// GetNode returns the node responsible for the given key
	GetNode(key string) (*Node, error)

	// GetNodes returns all nodes in the hash ring
	GetNodes() []*Node

	// GetNodeCount returns the number of nodes in the hash ring
	GetNodeCount() int

	// UpdateNodeStatus updates a node's status
	UpdateNodeStatus(nodeID string, status NodeStatus) error
}

Ring defines the interface for a consistent hash ring

Jump to

Keyboard shortcuts

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