github.com/fufuok/balancer@v1.0.0/examples/swrr/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/fufuok/balancer" 7 ) 8 9 func main() { 10 // Weighted node 11 wNodes := map[string]int{ 12 "A": 5, 13 "B": 1, 14 "C": 1, 15 "D": 0, 16 } 17 // lb := balancer.New(balancer.SmoothWeightedRoundRobin, wNodes) 18 lb := balancer.NewSmoothWeightedRoundRobin(wNodes) 19 fmt.Println("balancer name:", lb.Name()) 20 21 // result of smooth selection is: A A B A C A A 22 for i := 0; i < 7; i++ { 23 fmt.Print(lb.Select(), " ") 24 } 25 fmt.Println() 26 27 // add an item to be selected 28 lb.Add("E", 20) 29 // output: E 30 fmt.Println(lb.Select()) 31 32 // get all items 33 wNodes = lb.All().(map[string]int) 34 // map[A:5 B:1 C:1 D:0 E:20] 35 fmt.Printf("%+v\n", wNodes) 36 37 wNodes["E"] = 5 38 wNodes["D"] = 1 39 wNodes["A"] = 0 40 delete(wNodes, "B") 41 // reinitialize the balancer items 42 lb.Update(wNodes) 43 44 // result of smooth selection is: E E C E D E E E E C E D E E 45 for i := 0; i < 14; i++ { 46 fmt.Print(lb.Select(), " ") 47 } 48 fmt.Println() 49 50 // reset the balancer items weight 51 lb.Reset() 52 53 // remove an item 54 lb.Remove("E") 55 56 // remove all items 57 lb.RemoveAll() 58 }