consistent

package module
v0.0.0-...-ac7e943 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

README

consistent

This package of consistent is the most concise and efficient algorithm of consistent hash based on golang.

Example

Quick start:

package main

import (
	"fmt"
	"github.com/aylesford/consistent"
)

func main() {
	c := consistent.New()

	c.Add("aa", "bb", "cc")

	fmt.Println(
		c.Get("AAAA"), 
		c.Get("BBBB"), 
		c.Get("CCCC"), 
		c.Get("DDDD"),
		c.Get("EEEE"),
		c.Get("FFFF"),
	)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Represents how many virtual nodes are replicated on the hash ring in default.
	DefaultReplicas = 128

	// To convert items and keys into index.
	DefaultHashFunc = crc32.ChecksumIEEE
)

Functions

func WithHashFunc

func WithHashFunc(f func([]byte) uint32) option

WithHashFunc return a function to set the hashFunc of the consistent hash circle.

func WithReplicas

func WithReplicas(r int) option

WithReplicas return a function to set the replicas of the consistent hash circle.

Types

type Consistent

type Consistent struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Consistent holds the information about the members of the consistent hash circle.

func New

func New(options ...option) *Consistent

New returns a consistent hash circle according to your ideas.

func (*Consistent) Add

func (c *Consistent) Add(items ...string)

Add adds one or more items into the consistent hash circle.

func (*Consistent) Get

func (c *Consistent) Get(key string) string

Get returns a item according to the key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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