# smallnest/weighted

performant common weighted algorithm library
Switch branches/tags
Nothing to show
Latest commit e22997f Mar 1, 2018
 Failed to load latest commit information. .gitignore Dec 1, 2016 .travis.yml Feb 27, 2018 LICENSE Dec 1, 2016 Makefile Feb 27, 2018 README.md Mar 1, 2018 benchmark_test.go Feb 27, 2018 example_test.go Feb 27, 2018 random_weighted.go Mar 1, 2018 random_weighted_test.go Feb 27, 2018 roundrobin_weighted.go Mar 1, 2018 roundrobin_weighted_test.go Feb 27, 2018 smooth_weighted.go Mar 1, 2018 smooth_weighted_test.go Feb 27, 2018 weighted.go Mar 1, 2018

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 := &SW{}

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{}

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