consistent

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmptyCircle = errors.New("empty circle")

ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.

Functions

This section is empty.

Types

type Config added in v0.0.49

type Config struct {
	NumberOfReplicas int
}

type Consistent

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

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

func New

func New(opts ...Option) *Consistent

New creates a new Consistent object with a default setting of 20 replicas for each entry.

To change the number of replicas, set numberOfReplicas before adding entries.

Example
package main

import (
	"fmt"
	"github.com/sqjian/go-kit/consistent"
	"log"
)

func main() {
	c := consistent.New()
	c.Add("cacheA")
	c.Add("cacheB")
	c.Add("cacheC")
	users := []string{"user_mcnulty", "user_bunk", "user_omar", "user_bunny", "user_stringer"}
	for _, u := range users {
		server, err := c.Get(u)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s => %s\n", u, server)
	}
}
Output:

user_mcnulty => cacheA
user_bunk => cacheA
user_omar => cacheA
user_bunny => cacheC
user_stringer => cacheC

func (*Consistent) Add

func (c *Consistent) Add(elt string)

Add inserts a string element in the consistent hash.

func (*Consistent) Get

func (c *Consistent) Get(name string) (string, error)

Get returns an element close to where name hashes to in the circle.

func (*Consistent) GetN

func (c *Consistent) GetN(name string, n int) ([]string, error)

GetN returns the N closest distinct elements to the name input in the circle.

func (*Consistent) GetTwo

func (c *Consistent) GetTwo(name string) (string, string, error)

GetTwo returns the two closest distinct elements to the name input in the circle.

func (*Consistent) Members

func (c *Consistent) Members() []string

func (*Consistent) Remove

func (c *Consistent) Remove(elt string)

Remove removes an element from the hash.

func (*Consistent) Set

func (c *Consistent) Set(elts []string)

Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.

type Option added in v0.0.49

type Option func(*Config)

Jump to

Keyboard shortcuts

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