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