hashring

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Name            = "hashring"
	DefaultReplicas = 20
)

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

func New

func New(buckets []storage.Bucket, opts ...Option) (storage.Selector, error)

Types

type Balancer

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

func (*Balancer) Rebuild

func (b *Balancer) Rebuild(ctx context.Context, buckets []storage.Bucket) error

Rebuild implements storage.Selector.

func (*Balancer) Select

func (b *Balancer) Select(ctx context.Context, id *object.ID) storage.Bucket

Select implements storage.Selector.

type Consistent

type Consistent struct {
	sync.RWMutex // tavern 只有初始化时才写,其他都是读,故不需要用锁

	NumberOfReplicas int

	UseFnv bool
	// contains filtered or unexported fields
}

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

func NewConsistent

func NewConsistent(caches []Node, replicas int) *Consistent

NewConsistent 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.

func (*Consistent) Add

func (c *Consistent) Add(cache Node, weigth int)

Add inserts a string element in the consistent hash.

func (*Consistent) Get

func (c *Consistent) Get(name string) (Node, 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) ([]Node, error)

GetN returns the N 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(cache Node, weigth int)

Remove removes an element from the hash.

func (*Consistent) Set

func (c *Consistent) Set(caches []Node)

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

func (*Consistent) SetNumberOfReplicas

func (c *Consistent) SetNumberOfReplicas(num int)

type Node

type Node interface {
	ID() string
	Weight() int
}

type Option

type Option func(*Balancer)

func WithReplicas

func WithReplicas(replicas int) Option

WithReplicas ...

Jump to

Keyboard shortcuts

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