Help us shape the future of Go! Take the 2026 Go Developer Survey

consistent

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHosts = errors.New("no hosts added")

Functions

This section is empty.

Types

type Consistent

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

func NewConsistentHash

func NewConsistentHash(opts ...Option) *Consistent

func (*Consistent) Add

func (c *Consistent) Add(host string)

func (*Consistent) AddBatch added in v1.15.0

func (c *Consistent) AddBatch(hosts []string)

AddBatch adds hosts in input order, skipping existing hosts and preserving their loads. Hash collisions follow the same ownership rules as sequential Add calls. It holds the write lock for the entire batch and sorts at most once. An empty or nil slice is a no-op.

func (*Consistent) Done

func (c *Consistent) Done(host string)

Done Decrements the load of host by 1 should only be used with if you obtained a host with GetLeast

func (*Consistent) Get

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

Get It returns ErrNoHosts if the ring has no hosts in it

func (*Consistent) GetHash

func (c *Consistent) GetHash(hashKey uint32) (string, error)

GetHash It returns ErrNoHosts if the ring has no hosts in it

func (*Consistent) GetLeast

func (c *Consistent) GetLeast(key string) (string, error)

GetLeast It uses Consistent Hashing With Bounded loads https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html to pick the least loaded host that can serve the key It returns ErrNoHosts if the ring has no hosts in it.

func (*Consistent) GetLoads

func (c *Consistent) GetLoads() map[string]int64

GetLoads Returns the loads of all the hosts

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) Hash added in v1.14.0

func (c *Consistent) Hash(key string) uint32

func (*Consistent) Hosts

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

Hosts Return the list of hosts in the ring

func (*Consistent) Inc

func (c *Consistent) Inc(host string)

Inc Increments the load of host by 1 should only be used with if you obtained a host with GetLeast

func (*Consistent) MaxLoad

func (c *Consistent) MaxLoad() int64

MaxLoad Returns the maximum load of the single host which is: (total_load/number_of_hosts)*1.25 total_load = is the total number of active requests served by hosts for more info: https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

func (*Consistent) Members

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

func (*Consistent) Remove

func (c *Consistent) Remove(host string) bool

Remove Deletes host from the ring

func (*Consistent) RemoveBatch added in v1.15.0

func (c *Consistent) RemoveBatch(hosts []string)

RemoveBatch removes only positions currently owned by the supplied hosts. Unlike Remove, it preserves colliding positions owned by other hosts. Missing and duplicate hosts are ignored. It holds one write lock and compacts the index once; previously overwritten positions are not restored.

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.

func (*Consistent) SetHashFunc

func (c *Consistent) SetHashFunc(f HashFunc)

func (*Consistent) UpdateLoad

func (c *Consistent) UpdateLoad(host string, load int64)

UpdateLoad Sets the load of `host` to the given `load`

type HashFunc

type HashFunc func([]byte) uint64

type Host

type Host struct {
	Name string
	Load int64
}

type Option

type Option func(option *Options)

func WithHashFunc

func WithHashFunc(hash HashFunc) Option

func WithMaxVnodeNum

func WithMaxVnodeNum(maxVnodeNum int) Option

func WithReplicaNum

func WithReplicaNum(replicaNum int) Option

type Options

type Options struct {
	HashFunc    HashFunc
	ReplicaNum  int
	MaxVnodeNum int
}

Jump to

Keyboard shortcuts

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