weighted

package module
v0.0.0-...-36b780e Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: Apache-2.0 Imports: 2 Imported by: 20

README

License GoDoc travis coveralls Go Report Card

rust version: weighted-rs

Package weighted implements the smooth weighted round-robin balancing algorithm. This algorithm is implemented in Nginx: https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35.

Notice: The weighted is NOT goroutine-safe so you MUST use the synchronization primitive to protect it (the Next method) in concurrent cases.

Algorithm is as follows: on each peer selection we increase current_weight of each eligible peer by its weight, select peer with greatest current_weight and reduce its current_weight by total number of weight points distributed among peers.

In case of { 5, 1, 1 } weights this gives the following sequence of current_weight's: (a, a, b, a, c, a, a)

This is an example to use it:

package main

import "fmt"

func ExampleSW_Next() {
	w := &SW{}
	w.Add("a", 5)
	w.Add("b", 2)
	w.Add("c", 3)

	for i := 0; i < 10; i++ {
		fmt.Printf("%s ", w.Next())
	}
}

And this lib has provides another weighted round robin algorithm. This algorithm is used in LVS. It has better performance but it is not so more smooth than the first algorithm, so you can select one algorithm according to your case. It is used like the first:

package main

import "fmt"

func ExampleRRW_Next() {
	w := &RRW{}
	w.Add("a", 5)
	w.Add("b", 2)
	w.Add("c", 3)

	for i := 0; i < 10; i++ {
		fmt.Printf("%s ", w.Next())
	}
}

Documentation

Overview

Package weighted implements two weighted round robin algorithms. One is the smooth weighted round-robin balancing algorithm used in Nginx, and you can use "w := new SW{}" to use it. The other is wrr used in LVS and you can use "w := new RRW{}" to use it.

For Nginx smooth weighted round-robin balancing algorithm, you can check https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35. For LVS round-robin balancing algorithm, you can check http://kb.linuxvirtualitem.org/wiki/Weighted_Round-Robin_Scheduling.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RRW

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

RRW is a struct that contains weighted items implement LVS weighted round robin algorithm.

http://kb.linuxvirtualitem.org/wiki/Weighted_Round-Robin_Scheduling http://zh.linuxvirtualitem.org/node/37

func (*RRW) Add

func (w *RRW) Add(item interface{}, weight int)

Add a weighted item.

func (*RRW) All

func (w *RRW) All() map[interface{}]int

All returns all items.

func (*RRW) Next

func (w *RRW) Next() interface{}

Next returns next selected item.

Example
w := &RRW{}
w.Add("a", 5)
w.Add("b", 2)
w.Add("c", 3)

for i := 0; i < 10; i++ {
	fmt.Printf("%s ", w.Next())
}
Output:

a a a c a b c a b c

func (*RRW) RemoveAll

func (w *RRW) RemoveAll()

RemoveAll removes all weighted items.

func (*RRW) Reset

func (w *RRW) Reset()

Reset resets all current weights.

type RandW

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

RandW is a struct that contains weighted items implement weighted random algorithm.

func NewRandW

func NewRandW() *RandW

NewRandW creates a new RandW with a random object.

func (*RandW) Add

func (rw *RandW) Add(item interface{}, weight int)

Add adds a weighted item for selection.

func (*RandW) All

func (rw *RandW) All() map[interface{}]int

All returns all items.

func (*RandW) Next

func (rw *RandW) Next() (item interface{})

Next returns next selected item.

Example
w := NewRandW()
w.Add("a", 5)
w.Add("b", 2)
w.Add("c", 3)

for i := 0; i < 10; i++ {
	fmt.Printf("%s ", w.Next())
}
Output:

func (*RandW) RemoveAll

func (rw *RandW) RemoveAll()

RemoveAll removes all weighted items.

func (*RandW) Reset

func (rw *RandW) Reset()

Reset resets the balancing algorithm.

type SW

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

SW (Smooth Weighted) is a struct that contains weighted items and provides methods to select a weighted item. It is used for the smooth weighted round-robin balancing algorithm. This algorithm is implemented in Nginx: https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35.

Algorithm is as follows: on each peer selection we increase current_weight of each eligible peer by its weight, select peer with greatest current_weight and reduce its current_weight by total number of weight points distributed among peers.

In case of { 5, 1, 1 } weights this gives the following sequence of current_weight's: (a, a, b, a, c, a, a)

func (*SW) Add

func (w *SW) Add(item interface{}, weight int)

Add a weighted server.

func (*SW) All

func (w *SW) All() map[interface{}]int

All returns all items.

func (*SW) Next

func (w *SW) Next() interface{}

Next returns next selected server.

Example
w := &SW{}
w.Add("a", 5)
w.Add("b", 2)
w.Add("c", 3)

for i := 0; i < 10; i++ {
	fmt.Printf("%s ", w.Next())
}
Output:

a c b a a c a b c a

func (*SW) RemoveAll

func (w *SW) RemoveAll()

RemoveAll removes all weighted items.

func (*SW) Reset

func (w *SW) Reset()

Reset resets all current weights.

type W

type W interface {
	// Next gets next selected item.
	// Next is not goroutine-safe. You MUST use the snchronization primitive to protect it in concurrent cases.
	Next() (item interface{})
	// Add adds a weighted item for selection.
	Add(item interface{}, weight int)

	// All returns all items.
	All() map[interface{}]int

	// RemoveAll removes all weighted items.
	RemoveAll()
	// Reset resets the balancing algorithm.
	Reset()
}

W is a interface that implement a weighted round robin algorithm.

Jump to

Keyboard shortcuts

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