-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathproperties.go
469 lines (371 loc) · 13.9 KB
/
properties.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
Copyright 2016 The Smudge Authors.
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 smudge
import (
"fmt"
"net"
"os"
"regexp"
"strconv"
"strings"
)
// Provides a series of methods and constants that revolve around the getting
// (or programmatically setting/overriding) environmental properties, returning
// default values if not set.
const (
// EnvVarClusterName is the name of the environment variable the defines
// the name of the cluster. Multicast messages from differently-named
// instances are ignored.
EnvVarClusterName = "SMUDGE_CLUSTER_NAME"
// DefaultClusterName is the default name of the cluster for the purposes
// of multicast announcements: multicast messages from differently-named
// instances are ignored.
DefaultClusterName string = "smudge"
// EnvVarHeartbeatMillis is the name of the environment variable that
// sets the heartbeat frequency (in millis).
EnvVarHeartbeatMillis = "SMUDGE_HEARTBEAT_MILLIS"
// DefaultHeartbeatMillis is the default heartbeat frequency (in millis).
DefaultHeartbeatMillis int = 500
// EnvVarInitialHosts is the name of the environment variable that sets
// the initial known hosts. The value it sets should be a comma-delimitted
// string of one or more IP:PORT pairs (port is optional if it matched the
// value of SMUDGE_LISTEN_PORT).
EnvVarInitialHosts = "SMUDGE_INITIAL_HOSTS"
// DefaultInitialHosts default lists of initially known hosts.
DefaultInitialHosts string = ""
// EnvVarListenPort is the name of the environment variable that sets
// the UDP listen port.
EnvVarListenPort = "SMUDGE_LISTEN_PORT"
// DefaultListenPort is the default UDP listen port.
DefaultListenPort int = 9999
// EnvVarListenIP is the name of the environment variable that sets
// the listen IP.
EnvVarListenIP = "SMUDGE_LISTEN_IP"
// DefaultListenIP is the default listen IP.
DefaultListenIP = "127.0.0.1"
// EnvVarMaxBroadcastBytes is the name of the environment variable that
// the maximum byte length for broadcast payloads. Note that increasing
// this runs the risk of packet fragmentation and dropped messages.
EnvVarMaxBroadcastBytes = "SMUDGE_MAX_BROADCAST_BYTES"
// DefaultMaxBroadcastBytes is the default maximum byte length for
// broadcast payloads. This is guided by the maximum safe UDP packet size
// of 508 bytes, which must also contain status updates and additional
// message overhead.
DefaultMaxBroadcastBytes int = 256
// EnvVarMulticastAddress is the name of the environment variable that
// defines the multicast address that will be used.
EnvVarMulticastAddress = "SMUDGE_MULTICAST_ADDRESS"
// DefaultMulticastAddress is the default multicast address. Empty string
// indicates 224.0.0.0 for IPv4 and [ff02::1] for IPv6.
DefaultMulticastAddress string = ""
// EnvVarMulticastEnabled is the name of the environment variable that
// describes whether Smudge will attempt to announce its presence via
// multicast on startup.
EnvVarMulticastEnabled = "SMUDGE_MULTICAST_ENABLED"
// DefaultMulticastEnabled is the default value for whether Smudge will
// attempt to announce its presence via multicast on startup.
DefaultMulticastEnabled string = "true"
// EnvVarMulticastAnnounceIntervalSeconds is the name of the environment
// variable that describes whether Smudge will attempt to re-announce its
// presence via multicast every X seconds.
EnvVarMulticastAnnounceIntervalSeconds = "SMUDGE_MULTICAST_ANNOUNCE_INTERVAL"
// DefaultMulticastAnnounceIntervalSeconds is the default value for whether
// Smudge will re-announce its presence via multicast
DefaultMulticastAnnounceIntervalSeconds = 0
// EnvVarMulticastPort is the name of the environment variable that
// defines the multicast announcement listening port.
EnvVarMulticastPort = "SMUDGE_MULTICAST_PORT"
// DefaultMulticastPort is the default value for the multicast
// listening port.
DefaultMulticastPort int = 9998
// EnvVarPingHistoryFrontload is the name of the environment variable that
// defines the value (in milliseconds) used to pre-populate the ping
// history buffer, which is used to dynamically calculate ping timeouts and
// is gradually overwritten with real data over time.
EnvVarPingHistoryFrontload = "SMUDGE_PING_HISTORY_FRONTLOAD"
// DefaultPingHistoryFrontload is the default value (in milliseconds) used
// to pre-populate the ping history buffer, which is used to dynamically
// calculate ping timeouts and is gradually overwritten with real data
// over time.
DefaultPingHistoryFrontload = 200
// EnvVarMinPingTime is the name of the environment variable that
// defines the lower bound on recorded ping response times (in
// milliseconds). This prevents the system instability and flapping that
// can come from consistently small values.
EnvVarMinPingTime = "SMUDGE_MIN_PING_TIME"
// DefaultMinPingTime is default lower bound on recorded ping response
// times (in milliseconds). This prevents the system instability and
// flapping that can come from consistently small values.
DefaultMinPingTime = 150
)
var clusterName string
var heartbeatMillis int
var listenPort int
var listenIP net.IP
var initialHosts []string
var maxBroadcastBytes int
var minPingTime int
var multicastEnabledString string
var multicastEnabled = true
var multicastAnnounceIntervalSeconds = 10
var multicastPort int
var multicastAddress string
var pingHistoryFrontload int
const stringListDelimitRegex = "\\s*((,\\s*)|(\\s+))"
// GetClusterName gets the name of the cluster for the purposes of
// multicast announcements: multicast messages from differently-named
// instances are ignored.
func GetClusterName() string {
if clusterName == "" {
clusterName = getStringVar(EnvVarClusterName, DefaultClusterName)
}
return clusterName
}
// GetHeartbeatMillis gets this host's heartbeat frequency in milliseconds.
func GetHeartbeatMillis() int {
if heartbeatMillis == 0 {
heartbeatMillis = getIntVar(EnvVarHeartbeatMillis, DefaultHeartbeatMillis)
}
return heartbeatMillis
}
// GetInitialHosts returns the list of initially known hosts.
func GetInitialHosts() []string {
if initialHosts == nil {
initialHosts = getStringArrayVar(EnvVarInitialHosts, DefaultInitialHosts)
}
return initialHosts
}
// GetListenPort returns the port that this host will listen on.
func GetListenPort() int {
if listenPort == 0 {
listenPort = getIntVar(EnvVarListenPort, DefaultListenPort)
}
return listenPort
}
// GetListenIP returns the IP that this host will listen on.
func GetListenIP() net.IP {
if listenIP == nil {
listenIP = net.ParseIP(getStringVar(EnvVarListenIP, DefaultListenIP))
}
return listenIP
}
// GetMaxBroadcastBytes returns the maximum byte length for broadcast payloads.
func GetMaxBroadcastBytes() int {
if maxBroadcastBytes == 0 {
maxBroadcastBytes = getIntVar(EnvVarMaxBroadcastBytes, DefaultMaxBroadcastBytes)
}
return maxBroadcastBytes
}
// GetMinPingTime returns the minimum ping response time in milliseconds. Ping
// response times below this value are recorded as this minimum.
func GetMinPingTime() int {
if minPingTime == 0 {
minPingTime = getIntVar(EnvVarMinPingTime, DefaultMinPingTime)
}
return minPingTime
}
// GetMulticastEnabled returns whether multicast announcements are enabled.
func GetMulticastEnabled() bool {
if multicastEnabledString == "" {
multicastEnabledString = strings.ToLower(getStringVar(EnvVarMulticastEnabled, DefaultMulticastEnabled))
}
multicastEnabled = len(multicastEnabledString) > 0 && []rune(multicastEnabledString)[0] == 't'
return multicastEnabled
}
// GetMulticastAnnounceIntervalSeconds returns the amount of seconds to wait between
// multicast announcements.
func GetMulticastAnnounceIntervalSeconds() int {
if multicastAnnounceIntervalSeconds == 0 {
multicastAnnounceIntervalSeconds = getIntVar(EnvVarMulticastAnnounceIntervalSeconds, DefaultMulticastAnnounceIntervalSeconds)
}
return multicastAnnounceIntervalSeconds
}
// GetMulticastAddress returns the address the will be used for multicast
// announcements.
func GetMulticastAddress() string {
if multicastAddress == "" {
multicastAddress = getStringVar(EnvVarMulticastAddress, DefaultMulticastAddress)
}
return multicastAddress
}
// GetMulticastPort returns the defined multicast announcement listening port.
func GetMulticastPort() int {
if multicastPort == 0 {
multicastPort = getIntVar(EnvVarMulticastPort, DefaultMulticastPort)
}
return multicastPort
}
// GetPingHistoryFrontload returns the value (in milliseconds) used to
// pre-populate the ping history buffer, which is used to dynamically calculate
// ping timeouts and is gradually overwritten with real data over time.
func GetPingHistoryFrontload() int {
if pingHistoryFrontload == 0 {
pingHistoryFrontload = getIntVar(EnvVarPingHistoryFrontload, DefaultPingHistoryFrontload)
}
return pingHistoryFrontload
}
// SetClusterName sets the name of the cluster for the purposes of multicast
// announcements: multicast messages from differently-named instances are
// ignored.
func SetClusterName(val string) {
if val == "" {
clusterName = DefaultClusterName
} else {
clusterName = val
}
}
// SetHeartbeatMillis sets this nodes heartbeat frequency. Unlike
// SetListenPort(), calling this function after Begin() has been called will
// have an effect.
func SetHeartbeatMillis(val int) {
if val == 0 {
heartbeatMillis = DefaultHeartbeatMillis
} else {
heartbeatMillis = val
}
}
// SetListenPort sets the UDP port to listen on. It has no effect once
// Begin() has been called.
func SetListenPort(val int) {
if val == 0 {
listenPort = DefaultListenPort
} else {
listenPort = val
}
}
// SetListenIP sets the IP to listen on. It has no effect once
// Begin() has been called.
func SetListenIP(val net.IP) {
if len(AllNodes()) > 0 {
logWarn("Do not call SetListenIP() after nodes have been added, it may cause unexpected behavior.")
}
if val == nil {
listenIP = net.ParseIP(DefaultListenIP)
} else {
listenIP = val
}
}
// SetMaxBroadcastBytes sets the maximum byte length for broadcast payloads.
// Note that increasing this beyond the default of 256 runs the risk of packet
// fragmentation and dropped messages.
func SetMaxBroadcastBytes(val int) {
if val == 0 {
maxBroadcastBytes = DefaultMaxBroadcastBytes
} else {
maxBroadcastBytes = val
}
}
// SetMinPingTime sets the minimum ping response time in milliseconds. Ping
// response times below this value are recorded as this minimum.
func SetMinPingTime(val int) {
if val == 0 {
minPingTime = DefaultMinPingTime
} else {
minPingTime = val
}
}
// SetMulticastAddress sets the address that will be used for multicast
// announcements.
func SetMulticastAddress(val string) {
if val == "" {
multicastAddress = DefaultMulticastAddress
} else {
multicastAddress = val
}
}
// SetMulticastEnabled sets whether multicast announcements are enabled.
func SetMulticastEnabled(val bool) {
multicastEnabledString = fmt.Sprintf("%v", val)
}
// SetMulticastAnnounceIntervalSeconds sets the number of seconds between multicast announcements
func SetMulticastAnnounceIntervalSeconds(val int) {
multicastAnnounceIntervalSeconds = val
}
// SetMulticastPort sets multicast announcement listening port.
func SetMulticastPort(val int) {
if val == 0 {
multicastPort = DefaultMulticastPort
} else {
multicastPort = val
}
}
// SetPingHistoryFrontload sets the value (in milliseconds) used to
// pre-populate the ping history buffer, which is used to dynamically calculate
// ping timeouts and is gradually overwritten with real data over time.
// Setting this to 0 will restore the default value.
func SetPingHistoryFrontload(val int) {
if val == 0 {
pingHistoryFrontload = DefaultPingHistoryFrontload
} else {
pingHistoryFrontload = val
}
}
// Gets an environmental variable "key". If it does not exist, "defaultVal" is
// returned; if it does, it attempts to convert to an integer, returning
// "defaultVal" if it fails.
func getIntVar(key string, defaultVal int) int {
valueString := os.Getenv(key)
valueInt := defaultVal
if valueString != "" {
i, err := strconv.Atoi(valueString)
if err != nil {
logfWarn("Failed to parse env property %s: %s is not "+
"an integer. Using default.", key, valueString)
} else {
valueInt = i
}
}
return valueInt
}
// Gets an environmental variable "key". If it does not exist, "defaultVal" is
// returned; if it does, it attempts to convert to a string slice, returning
// "defaultVal" if it fails.
func getStringArrayVar(key string, defaultVal string) []string {
valueString := os.Getenv(key)
if valueString == "" {
valueString = defaultVal
}
valueSlice := splitDelimmitedString(valueString, stringListDelimitRegex)
return valueSlice
}
// Gets an environmental variable "key". If it does not exist, "defaultVal" is
// returned; if it does, it attempts to convert to a string, returning
// "defaultVal" if it fails.
func getStringVar(key string, defaultVal string) string {
valueString := os.Getenv(key)
if valueString == "" {
valueString = defaultVal
}
return valueString
}
// Splits a string on a regular expression.
func splitDelimmitedString(str string, regex string) []string {
var result []string
str = strings.TrimSpace(str)
if str != "" {
reg := regexp.MustCompile(regex)
indices := reg.FindAllStringIndex(str, -1)
result = make([]string, len(indices)+1)
lastStart := 0
for i, val := range indices {
result[i] = str[lastStart:val[0]]
lastStart = val[1]
}
result[len(indices)] = str[lastStart:]
// Special case of single empty string
if len(result) == 1 && result[0] == "" {
result = make([]string, 0, 0)
}
}
return result
}