/
listeners.go
175 lines (160 loc) · 6.5 KB
/
listeners.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
/*
* Copyright 2018 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"crypto/tls"
"net/http"
"time"
"github.com/tricksterproxy/trickster/pkg/config"
ph "github.com/tricksterproxy/trickster/pkg/proxy/handlers"
"github.com/tricksterproxy/trickster/pkg/proxy/listener"
ttls "github.com/tricksterproxy/trickster/pkg/proxy/tls"
"github.com/tricksterproxy/trickster/pkg/routing"
"github.com/tricksterproxy/trickster/pkg/tracing"
"github.com/tricksterproxy/trickster/pkg/util/log"
tl "github.com/tricksterproxy/trickster/pkg/util/log"
"github.com/tricksterproxy/trickster/pkg/util/metrics"
)
var lg = listener.NewListenerGroup()
func applyListenerConfigs(conf, oldConf *config.Config,
router, reloadHandler http.Handler, log *log.Logger,
tracers tracing.Tracers) {
var err error
var tlsConfig *tls.Config
if conf == nil || conf.Frontend == nil {
return
}
adminRouter := http.NewServeMux()
adminRouter.Handle(conf.ReloadConfig.HandlerPath, reloadHandler)
// No changes in frontend config
if oldConf != nil && oldConf.Frontend != nil &&
oldConf.Frontend.Equal(conf.Frontend) {
lg.UpdateFrontendRouters(router, adminRouter)
if ttls.OptionsChanged(conf, oldConf) {
tlsConfig, _ = conf.TLSCertConfig()
l := lg.Get("tlsListener")
if l != nil {
cs := l.CertSwapper()
if cs != nil {
cs.SetCerts(tlsConfig.Certificates)
}
}
}
}
if oldConf != nil && oldConf.Frontend.ConnectionsLimit != conf.Frontend.ConnectionsLimit {
log.Warn("connections limit change requires a process restart. listeners not updated.",
tl.Pairs{"oldLimit": oldConf.Frontend.ConnectionsLimit,
"newLimit": conf.Frontend.ConnectionsLimit})
return
}
hasOldFC := oldConf != nil && oldConf.Frontend != nil
hasOldMC := oldConf != nil && oldConf.Metrics != nil
hasOldRC := oldConf != nil && oldConf.ReloadConfig != nil
drainTimeout := time.Duration(conf.ReloadConfig.DrainTimeoutSecs) * time.Second
var tracerFlusherSet bool
// if TLS port is configured and at least one origin is mapped to a good tls config,
// then set up the tls server listener instance
if conf.Frontend.ServeTLS && conf.Frontend.TLSListenPort > 0 && (!hasOldFC ||
!oldConf.Frontend.ServeTLS ||
(oldConf.Frontend.TLSListenAddress != conf.Frontend.TLSListenAddress ||
oldConf.Frontend.TLSListenPort != conf.Frontend.TLSListenPort)) {
lg.DrainAndClose("tlsListener", drainTimeout)
tlsConfig, err = conf.TLSCertConfig()
if err != nil {
log.Error("unable to start tls listener due to certificate error", tl.Pairs{"detail": err})
} else {
wg.Add(1)
tracerFlusherSet = true
go lg.StartListener("tlsListener",
conf.Frontend.TLSListenAddress, conf.Frontend.TLSListenPort,
conf.Frontend.ConnectionsLimit, tlsConfig, router, wg, tracers, true,
time.Duration(conf.ReloadConfig.DrainTimeoutSecs)*time.Second, log)
}
} else if !conf.Frontend.ServeTLS && hasOldFC && oldConf.Frontend.ServeTLS {
// the TLS configs have been removed between the last config load and this one,
// the TLS listener port needs to be stopped
lg.DrainAndClose("tlsListener", drainTimeout)
} else if conf.Frontend.ServeTLS && ttls.OptionsChanged(conf, oldConf) {
tlsConfig, _ = conf.TLSCertConfig()
if err != nil {
log.Error("unable to update tls config to certificate error", tl.Pairs{"detail": err})
return
}
l := lg.Get("tlsListener")
if l != nil {
cs := l.CertSwapper()
if cs != nil {
cs.SetCerts(tlsConfig.Certificates)
}
}
}
// if the plaintext HTTP port is configured, then set up the http listener instance
if conf.Frontend.ListenPort > 0 && (!hasOldFC ||
(oldConf.Frontend.ListenAddress != conf.Frontend.ListenAddress ||
oldConf.Frontend.ListenPort != conf.Frontend.ListenPort)) {
lg.DrainAndClose("httpListener", drainTimeout)
wg.Add(1)
var t2 tracing.Tracers
if !tracerFlusherSet {
t2 = tracers
}
go lg.StartListener("httpListener",
conf.Frontend.ListenAddress, conf.Frontend.ListenPort,
conf.Frontend.ConnectionsLimit, nil, router, wg, t2, true, 0, log)
}
// if the Metrics HTTP port is configured, then set up the http listener instance
if conf.Metrics != nil && conf.Metrics.ListenPort > 0 &&
(!hasOldMC || (conf.Metrics.ListenAddress != oldConf.Metrics.ListenAddress ||
conf.Metrics.ListenPort != oldConf.Metrics.ListenPort)) {
lg.DrainAndClose("metricsListener", 0)
mr := http.NewServeMux()
mr.Handle("/metrics", metrics.Handler())
mr.HandleFunc(conf.Main.ConfigHandlerPath, ph.ConfigHandleFunc(conf))
if conf.Main.PprofServer == "both" || conf.Main.PprofServer == "metrics" {
routing.RegisterPprofRoutes("metrics", mr, log)
}
wg.Add(1)
go lg.StartListener("metricsListener",
conf.Metrics.ListenAddress, conf.Metrics.ListenPort,
conf.Frontend.ConnectionsLimit, nil, mr, wg, nil, true, 0, log)
} else {
mr := http.NewServeMux()
mr.Handle("/metrics", metrics.Handler())
mr.HandleFunc(conf.Main.ConfigHandlerPath, ph.ConfigHandleFunc(conf))
lg.UpdateRouter("metricsListener", mr)
}
// if the Reload HTTP port is configured, then set up the http listener instance
if conf.ReloadConfig != nil && conf.ReloadConfig.ListenPort > 0 &&
(!hasOldRC || (conf.ReloadConfig.ListenAddress != oldConf.ReloadConfig.ListenAddress ||
conf.ReloadConfig.ListenPort != oldConf.ReloadConfig.ListenPort)) {
wg.Add(1)
lg.DrainAndClose("reloadListener", time.Millisecond*500)
mr := http.NewServeMux()
mr.HandleFunc(conf.Main.ConfigHandlerPath, ph.ConfigHandleFunc(conf))
mr.Handle(conf.ReloadConfig.HandlerPath, reloadHandler)
if conf.Main.PprofServer == "both" || conf.Main.PprofServer == "reload" {
routing.RegisterPprofRoutes("reload", mr, log)
}
go lg.StartListener("reloadListener",
conf.ReloadConfig.ListenAddress, conf.ReloadConfig.ListenPort,
conf.Frontend.ConnectionsLimit, nil, mr, wg, nil, true, 0, log)
} else {
mr := http.NewServeMux()
mr.HandleFunc(conf.Main.ConfigHandlerPath, ph.ConfigHandleFunc(conf))
mr.Handle(conf.ReloadConfig.HandlerPath, reloadHandler)
lg.UpdateRouter("reloadListener", mr)
}
}