Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Consistent ¶
type Consistent interface { // Add provides the ability to add new nodes, returns an error message if the node does not exist. Add(string) error // Get returns the node corresponding to the current consistency hash Get(string) (string, error) // Del delete an already existing consistent hash node, returns an error message if the node does not exist. Del(string) error }
Consistent is the consistency hash interface, used to hide complex implementation details
func New ¶
func New() Consistent
New
Example ¶
package main import ( "fmt" "log" "github.com/dogslee/consistent" ) func main() { // default consistent hash function c := consistent.New() // add new node c.Add("node1") c.Add("node2") c.Add("node3") c.Add("node4") keyCase := []string{"user1", "user2", "user3", "user4"} for _, k := range keyCase { srvNode, err := c.Get(k) if err != nil { log.Fatal(err) } fmt.Printf("key: %s ==> srvNode: %s", k, srvNode) } }
Output: key: user1 ==> srvNode: node2 key: user2 ==> srvNode: node2 key: user3 ==> srvNode: node2 key: user4 ==> srvNode: node3
func NewOpt ¶
func NewOpt(opts ...Option) Consistent
NewOpt returns a custom set consistency hash. This defines the settings including: 1.the number of virtual node copies 2.basic string hash function 3.virtual node name generation rules
Example ¶
package main import ( "fmt" "hash/fnv" "log" "strconv" "github.com/dogslee/consistent" ) func main() { // custom consistent hash functions c := consistent.NewOpt( // set virtual node consistent.VirtualReplicas(50), // set hashFunc consistent.HashFunc(func(key string) (uint32, error) { h := fnv.New32a() h.Write([]byte(key)) return h.Sum32(), nil }), // set gen key rule consistent.KeyRule(func(key string, idx int) (string, error) { return key + strconv.Itoa(idx), nil })) c.Add("node1") c.Add("node2") c.Add("node3") c.Add("node4") keyCase := []string{"user10", "user20", "user30", "user40"} for _, k := range keyCase { srvNode, err := c.Get(k) if err != nil { log.Fatal(err) } fmt.Printf("key: %s ==> srvNode: %s\n", k, srvNode) } }
Output: key: user10 ==> srvNode: node3 key: user20 ==> srvNode: node1 key: user30 ==> srvNode: node2 key: user40 ==> srvNode: node2
type Option ¶
type Option func(o *consistent)
Option consistent hash setting function type
func KeyRule ¶
KeyRule set virtual node name generation rules. This function is generated by default as $key+"#"+string($idx)
func VirtualReplicas ¶
VirtualReplicas set the number of virtual node copies. This value defaults to 100
Click to show internal directories.
Click to hide internal directories.