/
main.go
175 lines (162 loc) · 3.63 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof" // NOTE: use http pprof
"os"
"os/signal"
"strings"
"syscall"
"overlord/lib/log"
"overlord/lib/prom"
"overlord/proxy"
)
const (
// VERSION version
VERSION = "1.5.2"
)
var (
version bool
check bool
logStd bool
logFile string
logVl int
debug bool
pprof string
metrics bool
config string
clusters clustersFlag
)
type clustersFlag []string
func (c *clustersFlag) String() string {
return strings.Join([]string(*c), " ")
}
func (c *clustersFlag) Set(n string) error {
*c = append(*c, n)
return nil
}
var usage = func() {
fmt.Fprintf(os.Stderr, "Usage of Overlord proxy:\n")
flag.PrintDefaults()
}
func init() {
flag.Usage = usage
flag.BoolVar(&check, "t", false, "conf file check")
flag.BoolVar(&version, "v", false, "print version.")
flag.BoolVar(&logStd, "std", false, "log will printing into stdout.")
flag.BoolVar(&debug, "debug", false, "debug model, will open stdout log. high priority than conf.debug.")
flag.StringVar(&logFile, "log", "", "log will printing file {log}. high priority than conf.log.")
flag.IntVar(&logVl, "log-vl", 0, "log verbose level. high priority than conf.log_vl.")
flag.StringVar(&pprof, "pprof", "", "pprof listen addr. high priority than conf.pprof.")
flag.BoolVar(&metrics, "metrics", false, "proxy support prometheus metrics and reuse pprof port.")
flag.StringVar(&config, "conf", "", "run with the specific configuration.")
flag.Var(&clusters, "cluster", "specify cache cluster configuration.")
}
func main() {
flag.Parse()
if version {
fmt.Printf("overlord version %s\n", VERSION)
os.Exit(0)
}
if check {
parseConfig()
os.Exit(0)
}
c, ccs := parseConfig()
if initLog(c) {
defer log.Close()
}
// new proxy
p, err := proxy.New(c)
if err != nil {
panic(err)
}
defer p.Close()
p.Serve(ccs)
// pprof
if c.Pprof != "" {
go http.ListenAndServe(c.Pprof, nil)
if c.Proxy.UseMetrics {
prom.Init()
} else {
prom.On = false
}
}
// hanlde signal
signalHandler()
}
func initLog(c *proxy.Config) bool {
var hs []log.Handler
if logStd || c.Debug {
hs = append(hs, log.NewStdHandler())
}
if c.Log != "" {
hs = append(hs, log.NewFileHandler(c.Log))
}
if len(hs) > 0 {
log.DefaultVerboseLevel = c.LogVL
log.Init(hs...)
return true
}
return false
}
func parseConfig() (c *proxy.Config, ccs []*proxy.ClusterConfig) {
if config != "" {
c = &proxy.Config{}
if err := c.LoadFromFile(config); err != nil {
panic(err)
}
} else {
c = proxy.DefaultConfig()
}
// high priority start
if pprof != "" {
c.Pprof = pprof
}
if metrics {
c.Proxy.UseMetrics = metrics
}
if debug {
c.Debug = debug
}
if logFile != "" {
c.Log = logFile
}
if logVl > 0 {
c.LogVL = logVl
}
// high priority end
checks := map[string]struct{}{}
for _, cluster := range clusters {
cs := &proxy.ClusterConfigs{}
if err := cs.LoadFromFile(cluster); err != nil {
panic(err)
}
for _, cc := range cs.Clusters {
if _, ok := checks[cc.Name]; ok {
panic("the same cluster name cannot be repeated")
}
checks[cc.Name] = struct{}{}
}
ccs = append(ccs, cs.Clusters...)
}
return
}
func signalHandler() {
var ch = make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
log.Infof("overlord proxy version[%s] start serving", VERSION)
si := <-ch
log.Infof("overlord proxy version[%s] signal(%s) stop the process", VERSION, si.String())
switch si {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
log.Infof("overlord proxy version[%s] exited", VERSION)
return
case syscall.SIGHUP:
default:
return
}
}
}