-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
101 lines (82 loc) · 1.62 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"github.com/golang/glog"
"os"
"os/signal"
"runtime/pprof"
"syscall"
)
const (
progname = "gogeta"
)
func getResolver(c *Config) (domainResolver, error) {
switch c.resolverType {
case "Dummy":
return &DummyResolver{}, nil
case "Env":
return NewEnvResolver(c), nil
default:
r, err := NewEtcdResolver(c)
if err != nil {
return nil, err
}
return r, nil
}
}
func main() {
glog.Infof("%s starting", progname)
c := parseConfig()
if c.cpuProfile != "" {
f, err := os.Create(c.cpuProfile)
if err != nil {
glog.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
handleSignals(c)
resolver, error := getResolver(c)
if error != nil {
panic(error)
} else {
resolver.init()
p := NewProxy(c, resolver)
p.start()
}
}
func handleSignals(config *Config) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
signal.Notify(signals, os.Interrupt, syscall.SIGUSR1)
signal.Notify(signals, os.Interrupt, syscall.SIGUSR2)
go func() {
isProfiling := false
defer func() {
if isProfiling {
pprof.StopCPUProfile()
}
}()
sig := <-signals
switch sig {
case syscall.SIGTERM, syscall.SIGINT:
//Exit gracefully
glog.Info("Shutting down...")
os.Exit(0)
case syscall.SIGUSR1:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
case syscall.SIGUSR2:
if !isProfiling {
f, err := os.Create(config.cpuProfile)
if err != nil {
glog.Fatal(err)
} else {
pprof.StartCPUProfile(f)
isProfiling = true
}
} else {
pprof.StopCPUProfile()
isProfiling = false
}
}
}()
}