maglev

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: Apache-2.0 Imports: 5 Imported by: 3

README

Maglev: A Google Maglev Hashing Algorithm implement in Golang

GoDoc Build Status Go

What is Maglev

Maglev is Google’s network load balancer. It is a large distributed software system that runs on commodity Linux servers. Unlike traditional hardware network load balancers, it does not require a specialized physical rack deployment, and its capacity can be easily adjusted by adding or removing servers. (cite from paper)

Here is a Chinese reading note about Maglev: [論文中文導讀] Maglev : A Fast and Reliable Software Network Load Balancer (using Consistent Hashing)

Installation and Usage

Install

go get github.com/kkdai/maglev

Usage


func main() {
	sizeN := 5
	lookupSizeM := 13 //(must be prime number)

	var names []string
	for i := 0; i < sizeN; i++ {
		names = append(names, fmt.Sprintf("backend-%d", i))
	}
	//backend-0 ~ backend-4 

	mm := NewMaglev(names, lookupSizeM)
	v, err := mm.Get("IP1")
	fmt.Println("node1:", v)
	//node1: backend-2
	v, _ = mm.Get("IP2")
	log.Println("node2:", v)
	//node2: backend-1
	v, _ = mm.Get("IPasdasdwni2")
	log.Println("node3:", v)
	//node3: backend-0

	if err := mm.Remove("backend-0"); err != nil {
		log.Fatal("Remove failed", err)
	}
	v, _ = mm.Get("IPasdasdwni2")
	log.Println("node3-D:", v)
	//node3-D: Change from "backend-0" to "backend-1"
}

Inspired By

Project52

It is one of my project 52.

License

This is under the Apache 2.0 license. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Maglev

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

Maglev :

func NewMaglev

func NewMaglev(backends []string, m uint64) (*Maglev, error)

NewMaglev :

func (*Maglev) Add

func (m *Maglev) Add(backend string) error

Add : Return nil if add success, otherwise return error

func (*Maglev) Clear

func (m *Maglev) Clear()

func (*Maglev) Get

func (m *Maglev) Get(obj string) (string, error)

Get :Get node name by object string.

func (*Maglev) Remove

func (m *Maglev) Remove(backend string) error

Remove :

func (*Maglev) Set

func (m *Maglev) Set(backends []string) error

Jump to

Keyboard shortcuts

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