core

package
v0.0.0-...-f4fb504 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package core

An implementation of Consistent Hashing Algorithm described in Golang

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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHostAlreadyExists = errors.New("host already exists")

	ErrHostNotFound = errors.New("host not found")
)

Functions

This section is empty.

Types

type Consistent

type Consistent struct {

	// the hash ring lock
	sync.RWMutex
	// contains filtered or unexported fields
}

Consistent is an implementation of consistent-hashing-algorithm

func NewConsistent

func NewConsistent(replicaNum int, hashFunc func(key string) uint64) *Consistent

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

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

func (*Consistent) GetKeyLeast

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

GetKeyLeast It uses consistent-hashing With Bounded loads to pick the least loaded host that can serve the key

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

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

func (*Consistent) GetLoads

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

GetLoads Returns the loads of all the hosts

func (*Consistent) Hosts

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

Hosts Return the list of real hosts

func (*Consistent) Inc

func (c *Consistent) Inc(hostName 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) RegisterHost

func (c *Consistent) RegisterHost(hostName string) error

func (*Consistent) UnregisterHost

func (c *Consistent) UnregisterHost(hostName string) error

func (*Consistent) UpdateLoad

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

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

type Host

type Host struct {
	// the host id: ip:port
	Name string

	// the load bound of the host
	LoadBound int64
}

Jump to

Keyboard shortcuts

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