This repository was archived by the owner on Aug 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconfig.go
More file actions
344 lines (276 loc) · 9.29 KB
/
config.go
File metadata and controls
344 lines (276 loc) · 9.29 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package client
import (
"encoding/json"
"reflect"
"sort"
)
const (
// Balancing schemes
RoundRobin = "RR"
LeastConn = "LC"
// Default timeout in milliseconds for clients and server connections
DefaultTimeout = 2000
// Default interval in milliseconds between health checks
DefaultCheckInterval = 5000
// Default network connections are TCP
DefaultNet = "tcp"
// All RoundRobin backends are weighted, with a default of 1
DefaultWeight = 1
// RoundRobin is the default balancing scheme
DefaultBalance = RoundRobin
// Default for Fall and Rise is 2
DefaultFall = 2
DefaultRise = 2
)
var (
// Status400s is a set of response codes to set an Error page for all 4xx responses.
Status400s = []int{400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418}
// Status500s is a set of response codes to set an Error page for all 5xx responses.
Status500s = []int{500, 501, 502, 503, 504, 505}
)
// Config is the global configuration for all Services.
// Defaults set here can be overridden by individual services.
type Config struct {
// Balance method
// Valid values are "RR" for RoundRobin, the default, and "LC" for
// LeastConnected.
Balance string `json:"balance,omitempty"`
// CheckInterval is in time in milliseconds between service health checks.
CheckInterval int `json:"check_interval"`
// Fall is the number of failed health checks before a service is marked
// down.
Fall int `json:"fall"`
// Rise is the number of successful health checks before a down service is
// marked up.
Rise int `json:"rise"`
// ClientTimeout is the maximum inactivity time, in milliseconds, for a
// connection to the client before it is closed.
ClientTimeout int `json:"client_timeout"`
// ServerTimeout is the maximum inactivity time, in milliseconds, for a
// connection to the backend before it is closed.
ServerTimeout int `json:"server_timeout"`
// DialTimeout is the timeout in milliseconds for connections to the
// backend service, including name resolution.
DialTimeout int `json:"connect_timeout"`
// HTTPSRedirect when set to true, redirects non-https request to https on
// all services. The request may either have Scheme set to 'https', or
// have an "X-Forwarded-Proto: https" header.
HTTPSRedirect bool `json:"https-redirect"`
// Services is a slice of ServiceConfig for each service. A service
// corresponds to one listening connection, and a number of backends to
// proxy.
Services []ServiceConfig `json:"services"`
}
// Marshal returns an entire config as a json []byte.
func (c *Config) Marshal() []byte {
sort.Sort(serviceSlice(c.Services))
js, _ := json.Marshal(c)
return js
}
// The string representation of a config is in json.
func (c *Config) String() string {
return string(c.Marshal())
}
// BackendConfig defines the parameters unique for individual backends.
type BackendConfig struct {
// Name must be unique for this service.
// Used for reference and for the HTTP API.
Name string `json:"name"`
// Addr must in the form ip:port
Addr string `json:"address"`
// Network must be "tcp" or "udp".
// Default is "tcp"
Network string `json:"network,omitempty"`
// CheckAddr must be in the form ip:port.
// A TCP connect is performed against this address to determine server
// availability. If this is empty, no checks will be performed.
CheckAddr string `json:"check_address"`
// Weight is always used for RoundRobin balancing. Default is 1
Weight int `json:"weight"`
}
// return a copy of the BackendConfig with default values set
func (b BackendConfig) SetDefaults() BackendConfig {
if b.Weight == 0 {
b.Weight = DefaultWeight
}
if b.Network == "" {
b.Network = DefaultNet
}
return b
}
func (b BackendConfig) Equal(other BackendConfig) bool {
b = b.SetDefaults()
other = other.SetDefaults()
return b == other
}
func (b *BackendConfig) Marshal() []byte {
js, _ := json.Marshal(b)
return js
}
func (b *BackendConfig) String() string {
return string(b.Marshal())
}
// keep things sorted for easy viewing and comparison
type backendSlice []BackendConfig
func (p backendSlice) Len() int { return len(p) }
func (p backendSlice) Less(i, j int) bool { return p[i].Name < p[j].Name }
func (p backendSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type serviceSlice []ServiceConfig
func (p serviceSlice) Len() int { return len(p) }
func (p serviceSlice) Less(i, j int) bool { return p[i].Name < p[j].Name }
func (p serviceSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Subset of service fields needed for configuration.
type ServiceConfig struct {
// Name is the unique name of the service. This is used only for reference
// and in the HTTP API.
Name string `json:"name"`
// Addr is the listening address for this service. Must be in the form
// "ip:addr"
Addr string `json:"address"`
// Network must be "tcp" or "udp".
// Default is "tcp"
Network string `json:"network,omitempty"`
// Balance method
// Valid values are "RR" for RoundRobin, the default, and "LC" for
// LeastConnected.
Balance string `json:"balance,omitempty"`
// CheckInterval is in time in milliseconds between service health checks.
CheckInterval int `json:"check_interval"`
// Fall is the number of failed health checks before a service is marked.
Fall int `json:"fall"`
// Rise is the number of successful health checks before a down service is
// marked up.
Rise int `json:"rise"`
// ClientTimeout is the maximum inactivity time, in milliseconds, for a
// connection to the client before it is closed.
ClientTimeout int `json:"client_timeout"`
// ServerTimeout is the maximum inactivity time, in milliseconds, for a
// connection to the backend before it is closed.
ServerTimeout int `json:"server_timeout"`
// DialTimeout is the timeout in milliseconds for connections to the
// backend service, including name resolution.
DialTimeout int `json:"connect_timeout"`
// HTTPSRedirect when set to true, redirects non-https request to https. The
// request may either have Scheme set to 'https', or have an
// "X-Forwarded-Proto: https" header.
HTTPSRedirect bool `json:"https-redirect"`
// Virtualhosts is a set of virtual hostnames for which this service should
// handle HTTP requests.
VirtualHosts []string `json:"virtual_hosts,omitempty"`
// ErrorPages are responses to be returned for HTTP error codes. Each page
// is defined by a URL mapped and is mapped to a list of error codes that
// should return the content at the URL. Error pages are retrieved ahead of
// time if possible, and cached.
ErrorPages map[string][]int `json:"error_pages,omitempty"`
// Backends is a list of all servers handling connections for this service.
Backends []BackendConfig `json:"backends,omitempty"`
// Maintenance mode is a flag to return 503 status codes to clients
// without visiting backends.
MaintenanceMode bool `json:"maintenance_mode"`
}
// Return a copy of ServiceConfig with any unset fields to their default
// values
func (s ServiceConfig) SetDefaults() ServiceConfig {
if s.Balance == "" {
s.Balance = DefaultBalance
}
if s.CheckInterval == 0 {
s.CheckInterval = DefaultCheckInterval
}
if s.Rise == 0 {
s.Rise = DefaultRise
}
if s.Fall == 0 {
s.Fall = DefaultFall
}
if s.Network == "" {
s.Network = DefaultNet
}
return s
}
// Compare a service's settings, ignoring individual backends.
func (s ServiceConfig) Equal(other ServiceConfig) bool {
// just remove the backends and compare the rest
s.Backends = nil
other.Backends = nil
s = s.SetDefaults()
other = other.SetDefaults()
sort.Strings(s.VirtualHosts)
sort.Strings(s.VirtualHosts)
// FIXME: ignoring VirtualHosts and ErrorPages equality
return reflect.DeepEqual(s, other)
}
// Check for equality including backends
func (s ServiceConfig) DeepEqual(other ServiceConfig) bool {
if len(s.Backends) != len(other.Backends) {
return false
}
if !s.Equal(other) {
return false
}
if len(s.Backends) != len(other.Backends) {
return false
}
sort.Sort(backendSlice(s.Backends))
sort.Sort(backendSlice(other.Backends))
for i := range s.Backends {
if !s.Backends[i].Equal(other.Backends[i]) {
return false
}
}
return true
}
func (b *ServiceConfig) Marshal() []byte {
sort.Sort(backendSlice(b.Backends))
js, _ := json.Marshal(b)
return js
}
func (b *ServiceConfig) String() string {
return string(b.Marshal())
}
// Create a new config by merging the values from the current config
// with those set in the new config
func (s ServiceConfig) Merge(cfg ServiceConfig) ServiceConfig {
new := s
// let's try not to change the name
new.Name = cfg.Name
if cfg.Addr != "" {
new.Addr = cfg.Addr
}
if cfg.Network != "" {
new.Network = cfg.Network
}
if cfg.Balance != "" {
new.Balance = cfg.Balance
}
if cfg.CheckInterval != 0 {
new.CheckInterval = cfg.CheckInterval
}
if cfg.Fall != 0 {
new.Fall = cfg.Fall
}
if cfg.Rise != 0 {
new.Rise = cfg.Rise
}
if cfg.ClientTimeout != 0 {
new.ClientTimeout = cfg.ClientTimeout
}
if cfg.ServerTimeout != 0 {
new.ServerTimeout = cfg.ServerTimeout
}
if cfg.DialTimeout != 0 {
new.DialTimeout = cfg.DialTimeout
}
if cfg.VirtualHosts != nil {
new.VirtualHosts = cfg.VirtualHosts
}
if cfg.ErrorPages != nil {
new.ErrorPages = cfg.ErrorPages
}
if cfg.Backends != nil {
new.Backends = cfg.Backends
}
new.HTTPSRedirect = cfg.HTTPSRedirect
new.MaintenanceMode = cfg.MaintenanceMode
return new
}