weighted

package module
v0.0.0-...-53e67ef Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2017 License: Apache-2.0 Imports: 1 Imported by: 0

README

License GoDoc travis Coverage Go Report Card

Package weighted implements 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)

This is an example to use it:

package main

import "fmt"

func ExampleW1_Next() {
	w := &W1{}
	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 ExampleW2_Next() {
	w := &W2{}
	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 W1{}" to use it. The other is wrr used in LVS and you can use "w := new W2{}" to use it.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type W

type W interface {
	Next() interface{}
	Add(server interface{}, weight int)
	RemoveAll()
	Reset()
}

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

type W1

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

W1 is struct that contains weighted servers and provides methods to select a weighted server. 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 (*W1) Add

func (w *W1) Add(server interface{}, weight int)

Add a weighted server.

func (*W1) Next

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

Next returns next selected server.

Example
w := &W1{}
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 (*W1) NextWeighted

func (w *W1) NextWeighted() *Weighted1

NextWeighted returns next selected weighted object.

func (*W1) RemoveAll

func (w *W1) RemoveAll()

RemoveAll removes all weighted servers.

func (*W1) Reset

func (w *W1) Reset()

Reset resets all current weights.

type W2

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

W2 is struct that contains weighted servers implement LVS weighted round robin algorithm.

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

func (*W2) Add

func (w *W2) Add(server interface{}, weight int)

Add a weighted server.

func (*W2) Next

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

Next returns next selected server.

Example
w := &W2{}
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 (*W2) RemoveAll

func (w *W2) RemoveAll()

RemoveAll removes all weighted servers.

func (*W2) Reset

func (w *W2) Reset()

Reset resets all current weights.

type W3

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

func (*W3) Add

func (w *W3) Add(server interface{}, weight int)

Add a weighted server.

func (*W3) Next

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

Next returns next selected server.

func (*W3) RemoveAll

func (w *W3) RemoveAll()

RemoveAll removes all weighted servers.

func (*W3) Reset

func (w *W3) Reset()

Reset resets all current weights.

type Weighted1

type Weighted1 struct {
	Server          interface{}
	Weight          int
	CurrentWeight   int
	EffectiveWeight int
}

Weighted1 is a wrapped server with weight

Jump to

Keyboard shortcuts

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