-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.go
313 lines (241 loc) · 6.41 KB
/
configuration.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
package main
import (
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
"github.com/sirupsen/logrus"
)
type Parameters struct {
enableHotReload bool
logLevel logrus.Level
configurationFile string
}
func loadParameters() Parameters {
parameters := Parameters{
enableHotReload: true,
logLevel: logrus.DebugLevel,
configurationFile: "",
}
configurationFile, exists := os.LookupEnv("CONFIGURATION_FILE")
if !exists {
log.Fatal("You must specify the CONFIGURATION_FILE environment variable")
}
parameters.configurationFile = configurationFile
enableHotReload, exists := os.LookupEnv("ENABLE_HOT_RELOAD")
if exists && enableHotReload == "false" {
parameters.enableHotReload = false
}
logLevel, exists := os.LookupEnv("LOG_LEVEL")
if exists {
switch logLevel {
case "info":
parameters.logLevel = logrus.InfoLevel
case "warn":
parameters.logLevel = logrus.WarnLevel
case "error":
parameters.logLevel = logrus.ErrorLevel
}
}
return parameters
}
type ConfigurationMethodExpose struct {
Pattern string
Name string
Capped uint64
Stream string
TimeoutWaitForNext string `yaml:"timeoutWaitForNext"`
}
type ConfigurationServiceExpose struct {
Service string
Consumer struct {
Concurrency uint32
Block string
Retry struct {
Limit int64
PageSize int64 `yaml:"pageSize"`
Deadline string
}
}
Client struct {
Address string
Port uint16
}
Methods []ConfigurationMethodExpose
}
type Configuration struct {
Redis struct {
Prefix string
JobPoolSize int `yaml:"jobPoolSize"`
Nodes []struct {
Sequence uint16
Host string
Port uint16
Db uint8
Password string
Sentinel bool
Master string
SentinelNodes []struct {
Host string
Port uint16
} `yaml:"sentinelNodes"`
}
}
Server struct {
Address string
Port uint16
Enable bool
MaxHeaderListSize uint32 `yaml:"maxHeaderListSize"`
}
Client struct {
Enable bool
}
Metrics struct {
Address string
ShutdownTimeout string `yaml:"shutdownTimeout"`
Port uint16
Routes struct {
Metrics string
Readiness string
Liveness string
}
}
Protos struct {
Services []ConfigurationServiceExpose
}
}
func loadConfiguration() Configuration {
content, err := ioutil.ReadFile(parameters.configurationFile)
if err != nil {
log.Fatalf("Error while reading configuration file: %v", err)
}
configuration := Configuration{}
err = yaml.Unmarshal(content, &configuration)
if err != nil {
log.Fatalf("Error while parsing YAML from configuration file: %v", err)
}
defaultRedisValues(&configuration)
defaultServerValues(&configuration)
defaultMetricsValues(&configuration)
defaultProtosValues(&configuration)
return configuration
}
func defaultRedisValues(conf *Configuration) {
if len(conf.Redis.Prefix) == 0 {
conf.Redis.Prefix = "polygate"
}
if conf.Redis.JobPoolSize <= 0 {
conf.Redis.JobPoolSize = 5
}
if len(conf.Redis.Nodes) == 0 {
log.Fatalf("You must specify at least one Redis node")
}
for i := range conf.Redis.Nodes {
node := &conf.Redis.Nodes[i]
if node.Sentinel {
if len(node.Master) == 0 {
log.Fatalf("Master name must be specified for sentinel, node %v", i)
}
if len(node.SentinelNodes) == 0 {
log.Fatalf("You must specify at least one sentinel node, node %v", i)
}
for t := range node.SentinelNodes {
sentinel := &node.SentinelNodes[t]
if len(sentinel.Host) == 0 {
log.Fatalf("Redis Sentinel node %v must have a valid address, node %v", t, i)
} else if sentinel.Port <= 0 {
log.Fatalf("Redis Sentinel node %v must have a valid port, node %v", t, i)
}
}
continue
}
if len(node.Host) == 0 {
log.Fatalf("Redis node %v must have a valid address", i)
} else if node.Port <= 0 {
log.Fatalf("Redis node %v must have a valid port", i)
}
}
}
func defaultServerValues(conf *Configuration) {
if len(conf.Server.Address) == 0 {
conf.Server.Address = "0.0.0.0"
}
if conf.Server.Port <= 0 {
conf.Server.Port = 4774
}
if conf.Server.MaxHeaderListSize <= 0 {
log.Warnf("A maxHeaderListSize of %v bytes will disable metadata support, do you really want this?", conf.Server.MaxHeaderListSize)
}
}
func defaultMetricsValues(conf *Configuration) {
if len(conf.Metrics.Address) == 0 {
conf.Metrics.Address = "0.0.0.0"
}
if len(conf.Metrics.ShutdownTimeout) == 0 {
conf.Metrics.ShutdownTimeout = "15"
}
if conf.Metrics.Port <= 0 {
conf.Metrics.Port = 2112
}
if len(conf.Metrics.Routes.Metrics) == 0 {
conf.Metrics.Routes.Metrics = "/metrics"
}
if len(conf.Metrics.Routes.Readiness) == 0 {
conf.Metrics.Routes.Readiness = "/ready"
}
if len(conf.Metrics.Routes.Liveness) == 0 {
conf.Metrics.Routes.Liveness = "/live"
}
}
func defaultProtosValues(conf *Configuration) {
if len(conf.Protos.Services) == 0 {
log.Fatal("You must specify at least one service")
}
for i := range conf.Protos.Services {
service := &conf.Protos.Services[i]
if len(service.Service) == 0 {
log.Fatalf("Empty service name for service %v", i)
}
if service.Consumer.Concurrency <= 0 {
service.Consumer.Concurrency = 50
}
if len(service.Consumer.Block) == 0 {
service.Consumer.Block = "5000ms"
}
if service.Consumer.Retry.Limit <= 0 {
service.Consumer.Retry.Limit = 3
}
if service.Consumer.Retry.PageSize <= 0 {
service.Consumer.Retry.PageSize = 1000
}
if len(service.Consumer.Retry.Deadline) == 0 {
service.Consumer.Retry.Deadline = "10000ms"
}
if len(service.Client.Address) == 0 {
log.Fatalf("Empty client address for service %v", i)
}
if service.Client.Port <= 0 {
log.Fatalf("Invalid client port for service %v", i)
}
if len(service.Methods) == 0 {
log.Fatalf("You must specify at least one method for service %v", i)
}
for t := range service.Methods {
method := &service.Methods[t]
if len(method.Name) == 0 {
log.Fatalf("Invalid method name for service %v, method %v", i, t)
}
if method.Pattern != "queue" && method.Pattern != "fireAndForget" {
log.Fatalf("Invalid method pattern for service %v, method %v", i, t)
}
if method.Capped <= 0 {
method.Capped = 10000
}
if len(method.Stream) == 0 {
method.Stream = method.Name
}
if len(method.TimeoutWaitForNext) == 0 {
method.TimeoutWaitForNext = "1s"
}
}
}
}