-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.4 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
65
66
67
68
69
70
71
72
73
74
package main
import (
"flag"
"github.com/coreos/etcd/client"
"golang.org/x/net/context"
"log"
"net/http"
"strings"
"time"
)
var c *string = flag.String("c", "./default.yml", "The config file path.")
func main() {
flag.Parse()
config, err := ReadConfig(*c)
if err != nil {
log.Fatalln("Read config error:", err.Error())
}
PATH := strings.TrimSuffix(config.ServiceDir, "/")
var cfg = client.Config{
Endpoints: config.EtcdEndpoints,
}
c, err := client.New(cfg)
if err != nil {
log.Fatalln(err.Error())
}
etcd := EtcdApi{
Api: client.NewKeysAPI(c),
Ctx: context.Background(),
Path: PATH,
}
uss, u_index, err := etcd.FetchUpstreamList()
if err != nil {
log.Fatalln("Fetch Upstream: ", err.Error())
}
ps, index, err := etcd.FetchUpstreamPeers(uss)
if err != nil {
log.Fatalln("Fetch Peers: ", err.Error())
}
if u_index > index {
index = u_index
}
go etcd.StartWatch(index, &ps)
chTask := make(chan *Peer)
chChecker := make(chan Checker, config.Concurrency)
chResult := make(chan CheckResult)
for i := 0; i < config.Concurrency; i++ {
c := Checker{Client: http.Client{Timeout: time.Duration(config.CheckTimeout) * time.Millisecond}}
chChecker <- c
}
go HandleResult(etcd, chResult, config)
go RunCheck(chChecker, chTask, chResult, config)
for {
for _, p := range ps {
chTask <- p
}
time.Sleep(time.Duration(config.CheckInterval) * time.Millisecond)
}
}