Documentation
¶
Index ¶
- type Config
- type ConsistentHash
- func (c *ConsistentHash) Add(node string)
- func (c *ConsistentHash) Distribute(keys []string) map[string][]string
- func (c *ConsistentHash) Distribution(keys []string) map[string]int
- func (c *ConsistentHash) Get(key string) (string, error)
- func (c *ConsistentHash) NodeCount() int
- func (c *ConsistentHash) Nodes() []string
- func (c *ConsistentHash) Remove(node string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ReplicationFactor is the number of virtual nodes (replicas) per physical node.
ReplicationFactor int
}
Config holds configuration for ConsistentHash.
type ConsistentHash ¶
type ConsistentHash struct {
// contains filtered or unexported fields
}
ConsistentHash implements consistent hashing. It is safe for concurrent use.
Example ¶
nodes := []string{"node1", "node2"}
c := consistent.NewWithNodes(nodes, consistent.Config{ReplicationFactor: 16})
node, err := c.Get("my-key")
if err != nil {
log.Fatal(err)
}
fmt.Println(node)
func NewWithNodes ¶
func NewWithNodes(nodes []string, config Config) *ConsistentHash
NewWithNodes creates a new ConsistentHash instance and adds the given nodes.
func (*ConsistentHash) Add ¶
func (c *ConsistentHash) Add(node string)
Add adds a physical node to the ring. Duplicate adds are ignored.
func (*ConsistentHash) Distribute ¶
func (c *ConsistentHash) Distribute(keys []string) map[string][]string
Distribute assigns each key to its responsible node and returns a mapping of node -> assigned keys. The returned map contains an entry for every node in the ring, even if it receives no keys.
func (*ConsistentHash) Distribution ¶
func (c *ConsistentHash) Distribution(keys []string) map[string]int
Distribution returns the number of keys assigned to each node.
func (*ConsistentHash) Get ¶
func (c *ConsistentHash) Get(key string) (string, error)
Get returns the node responsible for the given key. If the ring is empty, an error is returned.
func (*ConsistentHash) NodeCount ¶
func (c *ConsistentHash) NodeCount() int
NodeCount returns the number of physical nodes in the ring.
func (*ConsistentHash) Nodes ¶
func (c *ConsistentHash) Nodes() []string
Nodes returns a slice of all physical node names currently in the ring.
func (*ConsistentHash) Remove ¶
func (c *ConsistentHash) Remove(node string)
Remove removes a physical node from the ring.