-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (55 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"example.com/iptiq/balancer"
"example.com/iptiq/provider"
"fmt"
"time"
)
func main() {
runBalancerWithStrategy(balancer.StrategyRoundRobin)
runBalancerWithStrategy(balancer.StrategyRandom)
runEmptyBalancer()
}
func runBalancerWithStrategy(s balancer.Strategy) {
fmt.Println("-----------------------------------------")
fmt.Printf("Running balancer with strategy %s...\n", s)
b := balancer.New(s)
registerProviders(b)
exerciseBalancer(b)
exerciseFaultyProvider(b)
}
func runEmptyBalancer() {
fmt.Println("-----------------------------------------")
fmt.Printf("Running empty balancer...\n")
runGetRange(balancer.New(balancer.StrategyRoundRobin))
}
func registerProviders(b balancer.LoadBalancer) {
b.Register(&provider.SimpleProvider{Id: "simple1"})
b.Register(&provider.SimpleProvider{Id: "simple2"})
b.Register(&provider.SimpleProvider{Id: "simple3"})
b.Register(&provider.FaultyProvider{Id: "faulty1_permanent",
FaultDuration: 3000})
b.Register(&provider.FaultyProvider{Id: "faulty2_temporary",
FaultDuration: 2})
}
func exerciseBalancer(b balancer.LoadBalancer) {
b.Exclude("simple1")
runGetRange(b)
b.Include("simple1")
runGetRange(b)
}
func exerciseFaultyProvider(b balancer.LoadBalancer) {
fmt.Println("Make sure some health checks have passed...")
time.Sleep((2*balancer.CheckIntervalSeconds+1)*time.Second)
runGetRange(b)
}
func runGetRange(b balancer.LoadBalancer) {
for i:=0; i<5; i++ {
g, err := b.Get()
if err != nil {
fmt.Printf("Get() error = %v \n", err)
} else {
fmt.Printf("Get() = %s \n", g)
}
}
}