This package implement weighted round-robin load balancing algorithm, inspired from nginx.
It's works by increase all current_weight by their respective weight, then choose backend which has the largest current_weight, after that the previously selected backend will reduce its current_weight by total_weight, and this process will repeat every Next
method called.
In case you have 3 backend server A, B, C and its weight 2, 3, 2 the selected server will -> { B, A, C, B, A, C, B }
see pkg.go.dev
$ go get github.com/nothinux/weight
package main
import (
"github.com/nothinux/weight"
"log"
)
func main() {
w := weight.NewWeight()
w.AddWeight("A", 2)
w.AddWeight("B", 3)
w.AddWeight("C", 2)
for i := 0; i < 7; i++ {
log.Println(w.Next())
}
}