This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
config.go
259 lines (199 loc) · 8.61 KB
/
config.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
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"time"
"code.cloudfoundry.org/lager"
)
type SSL struct {
KeyFile string `json:"key_file"`
CertFile string `json:"cert_file"`
CACertFile string `json:"ca_file"`
}
type Config struct {
HeartbeatPeriod uint64 `json:"heartbeat_period_in_seconds"`
HeartbeatTTLInHeartbeats uint64 `json:"heartbeat_ttl_in_heartbeats"`
ActualFreshnessTTLInHeartbeats uint64 `json:"actual_freshness_ttl_in_heartbeats"`
GracePeriodInHeartbeats uint64 `json:"grace_period_in_heartbeats"`
DesiredFreshnessTTLInHeartbeats uint64 `json:"desired_freshness_ttl_in_heartbeats"`
SenderPollingIntervalInHeartbeats int `json:"sender_polling_interval_in_heartbeats"`
SenderTimeoutInHeartbeats int `json:"sender_timeout_in_heartbeats"`
FetcherPollingIntervalInHeartbeats int `json:"fetcher_polling_interval_in_heartbeats"`
FetcherTimeoutInHeartbeats int `json:"fetcher_timeout_in_heartbeats"`
ShredderPollingIntervalInHeartbeats int `json:"shredder_polling_interval_in_heartbeats"`
ShredderTimeoutInHeartbeats int `json:"shredder_timeout_in_heartbeats"`
AnalyzerPollingIntervalInHeartbeats int `json:"analyzer_polling_interval_in_heartbeats"`
AnalyzerTimeoutInHeartbeats int `json:"analyzer_timeout_in_heartbeats"`
ListenerHeartbeatSyncIntervalInMilliseconds int `json:"listener_heartbeat_sync_interval_in_milliseconds"`
StoreHeartbeatCacheRefreshIntervalInMilliseconds int `json:"store_heartbeat_cache_refresh_interval_in_milliseconds"`
DesiredStateBatchSize int `json:"desired_state_batch_size"`
FetcherNetworkTimeoutInSeconds int `json:"fetcher_network_timeout_in_seconds"`
ActualFreshnessKey string `json:"actual_freshness_key"`
DesiredFreshnessKey string `json:"desired_freshness_key"`
CCAuthUser string `json:"cc_auth_user"`
CCAuthPassword string `json:"cc_auth_password"`
CCBaseURL string `json:"cc_base_url"`
SkipSSLVerification bool `json:"skip_cert_verify"`
CCInternalURL string `json:"cc_internal_url"`
StoreSchemaVersion int `json:"store_schema_version"`
StoreURLs []string `json:"store_urls"`
StoreMaxConcurrentRequests int `json:"store_max_concurrent_requests"`
SenderNatsStartSubject string `json:"sender_nats_start_subject"`
SenderNatsStopSubject string `json:"sender_nats_stop_subject"`
SenderMessageLimit int `json:"sender_message_limit"`
NumberOfCrashesBeforeBackoffBegins int `json:"number_of_crashes_before_backoff_begins"`
StartingBackoffDelayInHeartbeats int `json:"starting_backoff_delay_in_heartbeats"`
MaximumBackoffDelayInHeartbeats int `json:"maximum_backoff_delay_in_heartbeats"`
MetricsServerPort int `json:"metrics_server_port"`
MetricsServerUser string `json:"metrics_server_user"`
MetricsServerPassword string `json:"metrics_server_password"`
APIServerAddress string `json:"api_server_address"`
APIServerPort int `json:"api_server_port"`
APIServerUsername string `json:"api_server_username"`
APIServerPassword string `json:"api_server_password"`
LogLevelString string `json:"log_level"`
NATS []struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
} `json:"nats"`
NatsClientPingInterval int `json:"nats_client_ping_interval"`
HttpHeartbeatServerAddress string `json:"http_heartbeat_server_address"`
HttpHeartbeatPort int `json:"http_heartbeat_port"`
SSLCerts SSL `json:"ssl"`
DropsondePort int `json:"dropsonde_port"`
ConsulCluster string `json:"config_cluster"`
ETCDRequireSSL bool `json:"etcd_require_ssl"`
ETCDSSLOptions SSL `json:"etcd"`
}
func defaults() Config {
return Config{
HeartbeatPeriod: 10, // TODO: convert to time.Duration
HeartbeatTTLInHeartbeats: 3,
ActualFreshnessTTLInHeartbeats: 3,
GracePeriodInHeartbeats: 3,
DesiredFreshnessTTLInHeartbeats: 12,
StoreMaxConcurrentRequests: 30,
SenderNatsStartSubject: "hm9000.start",
SenderNatsStopSubject: "hm9000.stop",
SenderMessageLimit: 60, // TODO: unit
SenderPollingIntervalInHeartbeats: 1, // why?
SenderTimeoutInHeartbeats: 10, // why?
FetcherPollingIntervalInHeartbeats: 6, // why?
FetcherTimeoutInHeartbeats: 60, // why?
ShredderPollingIntervalInHeartbeats: 360, // why?
ShredderTimeoutInHeartbeats: 6, // why?
AnalyzerPollingIntervalInHeartbeats: 1, // why?
AnalyzerTimeoutInHeartbeats: 10, // why?
NumberOfCrashesBeforeBackoffBegins: 3,
StartingBackoffDelayInHeartbeats: 3, // why?
MaximumBackoffDelayInHeartbeats: 96, // why?
ListenerHeartbeatSyncIntervalInMilliseconds: 1000, // TODO: convert to time.Duration
StoreHeartbeatCacheRefreshIntervalInMilliseconds: 60000, // TODO: convert to time.Duration
MetricsServerPort: 7879,
APIServerAddress: "0.0.0.0",
APIServerPort: 5155,
APIServerUsername: "magnet",
APIServerPassword: "orangutan4sale",
LogLevelString: "INFO",
ActualFreshnessKey: "/actual-fresh",
DesiredFreshnessKey: "/desired-fresh",
HttpHeartbeatServerAddress: "0.0.0.0",
HttpHeartbeatPort: 5335,
SSLCerts: SSL{},
ConsulCluster: "http://127.0.0.1:8500",
NatsClientPingInterval: 30,
}
}
func (conf *Config) HeartbeatDuration() time.Duration {
return time.Duration(conf.HeartbeatPeriod) * time.Second
}
func (conf *Config) HeartbeatTTL() uint64 {
return conf.HeartbeatTTLInHeartbeats * conf.HeartbeatPeriod
}
func (conf *Config) ActualFreshnessTTL() uint64 {
return conf.ActualFreshnessTTLInHeartbeats * conf.HeartbeatPeriod
}
func (conf *Config) GracePeriod() int {
return int(conf.GracePeriodInHeartbeats * conf.HeartbeatPeriod)
}
func (conf *Config) DesiredFreshnessTTL() uint64 {
return conf.DesiredFreshnessTTLInHeartbeats * conf.HeartbeatPeriod
}
func (conf *Config) FetcherNetworkTimeout() time.Duration {
return time.Duration(conf.FetcherNetworkTimeoutInSeconds) * time.Second
}
func (conf *Config) SenderPollingInterval() time.Duration {
return time.Duration(conf.SenderPollingIntervalInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) SenderTimeout() time.Duration {
return time.Duration(conf.SenderTimeoutInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) FetcherPollingInterval() time.Duration {
return time.Duration(conf.FetcherPollingIntervalInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) FetcherTimeout() time.Duration {
return time.Duration(conf.FetcherTimeoutInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) ShredderPollingInterval() time.Duration {
return time.Duration(conf.ShredderPollingIntervalInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) ShredderTimeout() time.Duration {
return time.Duration(conf.ShredderTimeoutInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) AnalyzerPollingInterval() time.Duration {
return time.Duration(conf.AnalyzerPollingIntervalInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) AnalyzerTimeout() time.Duration {
return time.Duration(conf.AnalyzerTimeoutInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) StartingBackoffDelay() time.Duration {
return time.Duration(conf.StartingBackoffDelayInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) MaximumBackoffDelay() time.Duration {
return time.Duration(conf.MaximumBackoffDelayInHeartbeats) * conf.HeartbeatDuration()
}
func (conf *Config) ListenerHeartbeatSyncInterval() time.Duration {
return time.Millisecond * time.Duration(conf.ListenerHeartbeatSyncIntervalInMilliseconds)
}
func (conf *Config) StoreHeartbeatCacheRefreshInterval() time.Duration {
return time.Millisecond * time.Duration(conf.StoreHeartbeatCacheRefreshIntervalInMilliseconds)
}
func (conf *Config) LogLevel() (lager.LogLevel, error) {
switch conf.LogLevelString {
case "INFO":
return lager.INFO, nil
case "DEBUG":
return lager.DEBUG, nil
case "":
return lager.INFO, nil
default:
return 0, errors.New(fmt.Sprintf("Unknown log level %s", conf.LogLevelString))
}
}
func DefaultConfig() (*Config, error) {
_, file, _, _ := runtime.Caller(0)
pathToJSON := filepath.Clean(filepath.Join(filepath.Dir(file), "default_config.json"))
return FromFile(pathToJSON)
}
func FromFile(path string) (*Config, error) {
json, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return FromJSON(json)
}
func FromJSON(JSON []byte) (*Config, error) {
config := defaults()
err := json.Unmarshal(JSON, &config)
if err == nil {
return &config, nil
} else {
return nil, err
}
}