github.com/fufuok/balancer@v1.0.0/examples/wr/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": 3, 14 "C": 1, 15 "D": 0, 16 } 17 // lb := balancer.New(balancer.WeightedRand, wNodes) 18 lb := balancer.NewWeightedRand(wNodes) 19 fmt.Println("balancer name:", lb.Name()) 20 21 for i := 0; i < 9; i++ { 22 fmt.Print(lb.Select(), " ") 23 } 24 fmt.Println() 25 26 // add an item to be selected 27 lb.Add("E", 20) 28 fmt.Println(lb.Select()) 29 30 // get all items 31 wNodes = lb.All().(map[string]int) 32 // map[A:5 B:3 C:1 D:0 E:20] 33 fmt.Printf("%+v\n", wNodes) 34 35 wNodes["E"] = 5 36 wNodes["D"] = 1 37 wNodes["A"] = 0 38 delete(wNodes, "B") 39 // reinitialize the balancer items 40 lb.Update(wNodes) 41 42 for i := 0; i < 14; i++ { 43 fmt.Print(lb.Select(), " ") 44 } 45 fmt.Println() 46 47 // reset the balancer items weight 48 lb.Reset() 49 50 // remove an item 51 lb.Remove("E") 52 53 // remove all items 54 lb.RemoveAll() 55 }