github.com/fufuok/balancer@v1.0.0/examples/new/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/fufuok/balancer" 7 ) 8 9 func main() { 10 var lb balancer.Balancer 11 12 // SmoothWeightedRoundRobin / WeightedRoundRobin / WeightedRand use map[string]int 13 wNodes := map[string]int{ 14 "A": 5, 15 "B": 1, 16 "C": 1, 17 "D": 0, 18 } 19 lb = balancer.New(balancer.WeightedRoundRobin, wNodes, nil) 20 fmt.Println("balancer name:", lb.Name()) 21 22 lb = balancer.New(balancer.WeightedRand, wNodes, nil) 23 fmt.Println("balancer name:", lb.Name()) 24 25 lb = balancer.New(balancer.SmoothWeightedRoundRobin, wNodes, nil) 26 fmt.Println("balancer name:", lb.Name()) 27 28 // result of smooth selection is similar to: A A C A B A A 29 for i := 0; i < 7; i++ { 30 fmt.Print(lb.Select(), " ") 31 } 32 fmt.Println() 33 34 // RoundRobin / Random / ConsistentHash use []string 35 nodes := []string{"A", "B", "C"} 36 lb = balancer.New(balancer.ConsistentHash, nil, nodes) 37 fmt.Println("balancer name:", lb.Name()) 38 39 lb = balancer.New(balancer.Random, nil, nodes) 40 fmt.Println("balancer name:", lb.Name()) 41 42 lb = balancer.New(balancer.RoundRobin, nil, nodes) 43 fmt.Println("balancer name:", lb.Name()) 44 45 // result of RoundRobin: A B C A B C A 46 for i := 0; i < 7; i++ { 47 fmt.Print(lb.Select(), " ") 48 } 49 fmt.Println() 50 51 // same effect 52 lb = balancer.New(balancer.SmoothWeightedRoundRobin, nil, nil) 53 lb.Add("A", 1) 54 lb.Select() 55 56 // or like this 57 lb = balancer.New(balancer.RoundRobin, nil, nil) 58 lb.Update(nodes) 59 lb.Select() 60 }