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

consistent

package
v2.14.21 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

An implementation of Consistent Hashing and Consistent Hashing With Bounded Loads.

https://en.wikipedia.org/wiki/Consistent_hashing

https://research.googleblog.com/2017/04/consistent-hashing-with-bounded-loads.html

Index

Constants

View Source
const OptimalExtraCapacityFactor = 1.25

OptimalExtraCapacityFactor extra factor capacity (1 + ε). The ideal balance between keeping the shards uniform while also keeping consistency when changing shard numbers.

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 New

func New() *Consistent

func NewWithReplicationFactor

func NewWithReplicationFactor(replicationFactor int) *Consistent

func (*Consistent) Add

func (c *Consistent) Add(server string)

func (*Consistent) Done

func (c *Consistent) Done(server string)

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(client string) (string, error)

Get returns the server that owns the given client. As described in https://en.wikipedia.org/wiki/Consistent_hashing It returns ErrNoHosts if the ring has no servers in it.

func (*Consistent) GetLeast

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

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

func (*Consistent) GetLoads

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

Returns the loads of all the hosts

func (*Consistent) Inc

func (c *Consistent) Inc(server string)

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

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) Remove

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

Deletes host from the ring

func (*Consistent) Servers

func (c *Consistent) Servers() (servers []string)

Return the list of servers in the ring

func (*Consistent) UpdateLoad

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

Sets the load of `server` to the given `load`

type Host

type Host struct {
	Name string
	Load int64
}

Jump to

Keyboard shortcuts

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