consistent

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 6 Imported by: 0

README

consistent

🐶🐶

this package is consistent hash for golang.

install

go get github.com/dogslee/consistent

example

Lock at the godoc

status

This package is a stable version though. but is still a personal and experimental tool. It is friendly for developers with customization needs. Interested developers can change it with their extensive mathematical knowledge.

Thanks

😄

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 HashFunc

func HashFunc(f func(string) (uint32, error)) Option

HashFunc set string hash function operation. This function uses CRC-32 by default.

func KeyRule

func KeyRule(f func(string, int) (string, error)) Option

KeyRule set virtual node name generation rules. This function is generated by default as $key+"#"+string($idx)

func VirtualReplicas

func VirtualReplicas(n int) Option

VirtualReplicas set the number of virtual node copies. This value defaults to 100

Jump to

Keyboard shortcuts

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