forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (166 loc) · 4.37 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"runtime"
"runtime/debug"
"github.com/eBay/fabio/admin"
"github.com/eBay/fabio/config"
"github.com/eBay/fabio/exit"
"github.com/eBay/fabio/metrics"
"github.com/eBay/fabio/proxy"
"github.com/eBay/fabio/registry"
"github.com/eBay/fabio/registry/consul"
"github.com/eBay/fabio/registry/file"
"github.com/eBay/fabio/registry/static"
"github.com/eBay/fabio/route"
)
// version contains the version number
//
// It is set by build/release.sh for tagged releases
// so that 'go get' just works.
//
// It is also set by the linker when fabio
// is built via the Makefile or the build/docker.sh
// script to ensure the correct version nubmer
var version = "1.3.3"
func main() {
cfg, err := config.Load()
if err != nil {
exit.Fatalf("[FATAL] %s. %s", version, err)
}
if cfg == nil {
fmt.Println(version)
return
}
log.Printf("[INFO] Runtime config\n" + toJSON(cfg))
log.Printf("[INFO] Version %s starting", version)
log.Printf("[INFO] Go runtime is %s", runtime.Version())
exit.Listen(func(s os.Signal) {
if registry.Default == nil {
return
}
registry.Default.Deregister()
})
httpProxy := newHTTPProxy(cfg)
tcpProxy := proxy.NewTCPSNIProxy(cfg.Proxy)
initRuntime(cfg)
initMetrics(cfg)
initBackend(cfg)
go watchBackend()
startAdmin(cfg)
startListeners(cfg.Listen, cfg.Proxy.ShutdownWait, httpProxy, tcpProxy)
exit.Wait()
}
func newHTTPProxy(cfg *config.Config) http.Handler {
if err := route.SetPickerStrategy(cfg.Proxy.Strategy); err != nil {
exit.Fatal("[FATAL] ", err)
}
log.Printf("[INFO] Using routing strategy %q", cfg.Proxy.Strategy)
if err := route.SetMatcher(cfg.Proxy.Matcher); err != nil {
exit.Fatal("[FATAL] ", err)
}
log.Printf("[INFO] Using routing matching %q", cfg.Proxy.Matcher)
tr := &http.Transport{
ResponseHeaderTimeout: cfg.Proxy.ResponseHeaderTimeout,
MaxIdleConnsPerHost: cfg.Proxy.MaxConn,
Dial: (&net.Dialer{
Timeout: cfg.Proxy.DialTimeout,
KeepAlive: cfg.Proxy.KeepAliveTimeout,
}).Dial,
}
return proxy.NewHTTPProxy(tr, cfg.Proxy)
}
func startAdmin(cfg *config.Config) {
log.Printf("[INFO] Admin server listening on %q", cfg.UI.Addr)
go func() {
if err := admin.ListenAndServe(cfg, version); err != nil {
exit.Fatal("[FATAL] ui: ", err)
}
}()
}
func initMetrics(cfg *config.Config) {
if cfg.Metrics.Target == "" {
log.Printf("[INFO] Metrics disabled")
return
}
var err error
if metrics.DefaultRegistry, err = metrics.NewRegistry(cfg.Metrics); err != nil {
exit.Fatal("[FATAL] ", err)
}
if route.ServiceRegistry, err = metrics.NewRegistry(cfg.Metrics); err != nil {
exit.Fatal("[FATAL] ", err)
}
}
func initRuntime(cfg *config.Config) {
if os.Getenv("GOGC") == "" {
log.Print("[INFO] Setting GOGC=", cfg.Runtime.GOGC)
debug.SetGCPercent(cfg.Runtime.GOGC)
} else {
log.Print("[INFO] Using GOGC=", os.Getenv("GOGC"), " from env")
}
if os.Getenv("GOMAXPROCS") == "" {
log.Print("[INFO] Setting GOMAXPROCS=", cfg.Runtime.GOMAXPROCS)
runtime.GOMAXPROCS(cfg.Runtime.GOMAXPROCS)
} else {
log.Print("[INFO] Using GOMAXPROCS=", os.Getenv("GOMAXPROCS"), " from env")
}
}
func initBackend(cfg *config.Config) {
var err error
switch cfg.Registry.Backend {
case "file":
registry.Default, err = file.NewBackend(cfg.Registry.File.Path)
case "static":
registry.Default, err = static.NewBackend(cfg.Registry.Static.Routes)
case "consul":
registry.Default, err = consul.NewBackend(&cfg.Registry.Consul)
default:
exit.Fatal("[FATAL] Unknown registry backend ", cfg.Registry.Backend)
}
if err != nil {
exit.Fatal("[FATAL] Error initializing backend. ", err)
}
if err := registry.Default.Register(); err != nil {
exit.Fatal("[FATAL] Error registering backend. ", err)
}
}
func watchBackend() {
var (
last string
svccfg string
mancfg string
)
svc := registry.Default.WatchServices()
man := registry.Default.WatchManual()
for {
select {
case svccfg = <-svc:
case mancfg = <-man:
}
// manual config overrides service config
// order matters
next := svccfg + "\n" + mancfg
if next == last {
continue
}
t, err := route.ParseString(next)
if err != nil {
log.Printf("[WARN] %s", err)
continue
}
route.SetTable(t)
last = next
}
}
func toJSON(v interface{}) string {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic("json: " + err.Error())
}
return string(data)
}