This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
325 lines (282 loc) · 10.4 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
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
/*
(c) Copyright NetFoundry Inc.
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
https://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 xlink_transport
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/fabric/router/link"
"github.com/openziti/transport/v2"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"reflect"
"time"
)
const (
MinRetryInterval = 10 * time.Millisecond
MaxRetryInterval = 24 * time.Hour
MinRetryBackoffFactor = 1
MaxRetryBackoffFactor = 100
DefaultHealthyMinRetryInterval = 5 * time.Second
DefaultHealthyMaxRetryInterval = 5 * time.Minute
DefaultHealthyRetryBackoffFactor = 1.5
DefaultUnhealthyMinRetryInterval = time.Minute
DefaultUnhealthyMaxRetryInterval = time.Hour
DefaultUnhealthyRetryBackoffFactor = 10
)
func loadListenerConfig(data map[interface{}]interface{}) (*listenerConfig, error) {
config := &listenerConfig{}
if value, found := data["bind"]; found {
if addressString, ok := value.(string); ok {
if address, err := transport.ParseAddress(addressString); err == nil {
config.bind = address
config.advertise = address
config.linkProtocol = address.Type()
} else {
return nil, fmt.Errorf("error parsing 'bind' address in listener config (%w)", err)
}
} else {
return nil, fmt.Errorf("invalid 'bind' address in listener config (%s)", reflect.TypeOf(value))
}
} else {
return nil, fmt.Errorf("missing 'bind' address in listener config")
}
if value, found := data["bindInterface"]; found {
if addressString, ok := value.(string); ok {
config.bindInterface = addressString
} else {
return nil, fmt.Errorf("invalid 'bindInterface' address in listener config (%T)", value)
}
} else {
if addr, ok := config.bind.(transport.HostPortAddress); ok {
intf, err := transport.ResolveInterface(addr.Hostname())
if err != nil {
pfxlog.Logger().WithError(err).WithField("addr", addr.String()).Warn("unable to get interface for address")
} else {
pfxlog.Logger().Infof("found interface %v for bind address %v", intf.Name, addr.String())
config.bindInterface = intf.Name
}
}
}
if value, found := data["advertise"]; found {
if addressString, ok := value.(string); ok {
if address, err := transport.ParseAddress(addressString); err == nil {
config.advertise = address
} else {
return nil, fmt.Errorf("error parsing 'advertise' address in listener config")
}
} else {
return nil, fmt.Errorf("invalid 'advertise' address in listener config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["costTags"]; found {
if costTags, ok := value.([]interface{}); ok {
for _, tag := range costTags {
config.linkCostTags = append(config.linkCostTags, fmt.Sprint(tag))
}
} else {
return nil, fmt.Errorf("invalid 'costTags' value in listener config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["groups"]; found {
if group, ok := value.(string); ok {
config.groups = append(config.groups, group)
} else if groups, ok := value.([]interface{}); ok {
for _, group := range groups {
config.groups = append(config.groups, fmt.Sprint(group))
}
} else {
return nil, fmt.Errorf("invalid 'groups' value in listener config (%s)", reflect.TypeOf(value))
}
}
if len(config.groups) == 0 {
config.groups = append(config.groups, link.GroupDefault)
}
if value, found := data["options"]; found {
if submap, ok := value.(map[interface{}]interface{}); ok {
options, err := channel.LoadOptions(submap)
if err != nil {
return nil, errors.Wrap(err, "failed to parse link listener options")
}
config.options = options
} else {
return nil, fmt.Errorf("invalid 'options' in listener config (%s)", reflect.TypeOf(value))
}
} else {
config.options = channel.DefaultOptions()
}
return config, nil
}
type listenerConfig struct {
bind transport.Address
advertise transport.Address
bindInterface string
linkProtocol string
linkCostTags []string
groups []string
options *channel.Options
}
func loadDialerConfig(data map[interface{}]interface{}) (*dialerConfig, error) {
config := &dialerConfig{split: true}
if value, found := data["split"]; found {
if split, ok := value.(bool); ok {
config.split = split
} else {
return nil, errors.Errorf("invalid 'split' flag in dialer config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["bind"]; found {
logrus.Debugf("Parsing dialer bind config")
if addressString, ok := value.(string); ok {
_, err := transport.ResolveInterface(addressString)
if err != nil {
return nil, errors.Errorf("invalid 'bind' address in dialer config (%s)", err)
}
config.localBinding = addressString
logrus.Debugf("Using local bind address %s", config.localBinding)
} else {
return nil, fmt.Errorf("invalid 'bind' address in dialer config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["groups"]; found {
if group, ok := value.(string); ok {
config.groups = append(config.groups, group)
} else if groups, ok := value.([]interface{}); ok {
for _, group := range groups {
config.groups = append(config.groups, fmt.Sprint(group))
}
} else {
return nil, fmt.Errorf("invalid 'groups' value in listener config (%s)", reflect.TypeOf(value))
}
}
if len(config.groups) == 0 {
config.groups = append(config.groups, link.GroupDefault)
}
config.healthyBackoffConfig = &backoffConfig{
minRetryInterval: DefaultHealthyMinRetryInterval,
maxRetryInterval: DefaultHealthyMaxRetryInterval,
retryBackoffFactor: DefaultHealthyRetryBackoffFactor,
}
config.unhealthyBackoffConfig = &backoffConfig{
minRetryInterval: DefaultUnhealthyMinRetryInterval,
maxRetryInterval: DefaultUnhealthyMaxRetryInterval,
retryBackoffFactor: DefaultUnhealthyRetryBackoffFactor,
}
if value, found := data["healthyDialBackoff"]; found {
if submap, ok := value.(map[interface{}]interface{}); ok {
if err := config.healthyBackoffConfig.load(submap); err != nil {
return nil, errors.Wrap(err, "failed to parse healthyDialBackoff config")
}
} else {
return nil, fmt.Errorf("invalid 'healthyDialBackoff' in dialer config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["unhealthyDialBackoff"]; found {
if submap, ok := value.(map[interface{}]interface{}); ok {
if err := config.unhealthyBackoffConfig.load(submap); err != nil {
return nil, errors.Wrap(err, "failed to parse unhealthyDialBackoff config")
}
} else {
return nil, fmt.Errorf("invalid 'healthyDialBackoff' in dialer config (%s)", reflect.TypeOf(value))
}
}
if value, found := data["options"]; found {
if submap, ok := value.(map[interface{}]interface{}); ok {
options, err := channel.LoadOptions(submap)
if err != nil {
return nil, errors.Wrap(err, "failed to parse link dialer options")
}
config.options = options
} else {
return nil, fmt.Errorf("invalid 'options' in dialer config (%s)", reflect.TypeOf(value))
}
}
return config, nil
}
type backoffConfig struct {
minRetryInterval time.Duration
maxRetryInterval time.Duration
retryBackoffFactor float64
}
func (self *backoffConfig) GetMinRetryInterval() time.Duration {
return self.minRetryInterval
}
func (self *backoffConfig) GetMaxRetryInterval() time.Duration {
return self.maxRetryInterval
}
func (self *backoffConfig) GetRetryBackoffFactor() float64 {
return self.retryBackoffFactor
}
func (self *backoffConfig) load(data map[interface{}]interface{}) error {
if value, found := data["retryBackoffFactor"]; found {
if floatValue, ok := value.(float64); ok {
self.retryBackoffFactor = floatValue
} else if intValue, ok := value.(int); ok {
self.retryBackoffFactor = float64(intValue)
} else {
return errors.Errorf("invalid (non-numeric) value for retryBackoffFactor: %v", value)
}
if self.retryBackoffFactor < MinRetryBackoffFactor {
return errors.Errorf("retryBackoffFactor of %v is lower than minimum value of %v", self.retryBackoffFactor, MinRetryBackoffFactor)
}
if self.retryBackoffFactor > MaxRetryBackoffFactor {
return errors.Errorf("retryBackoffFactor of %v is larger than maximum value of %v", self.retryBackoffFactor, MaxRetryBackoffFactor)
}
}
if value, found := data["minRetryInterval"]; found {
if strVal, ok := value.(string); ok {
if d, err := time.ParseDuration(strVal); err == nil {
self.minRetryInterval = d
} else {
return errors.Wrapf(err, "invalid value for minRetryInterval: %v", value)
}
} else {
return errors.Errorf("invalid (non-string) value for minRetryInterval: %v", value)
}
if self.minRetryInterval < MinRetryInterval {
return errors.Errorf("minRetryInterval of %v is lower than minimum value of %v", self.minRetryInterval, MinRetryInterval)
}
if self.minRetryInterval > MaxRetryInterval {
return errors.Errorf("minRetryInterval of %v is larger than maximum value of %v", self.minRetryInterval, MaxRetryInterval)
}
}
if value, found := data["maxRetryInterval"]; found {
if strVal, ok := value.(string); ok {
if d, err := time.ParseDuration(strVal); err == nil {
self.maxRetryInterval = d
} else {
return errors.Wrapf(err, "invalid value for maxRetryInterval: %v", value)
}
} else {
return errors.Errorf("invalid (non-string) value for maxRetryInterval: %v", value)
}
if self.maxRetryInterval < MinRetryInterval {
return errors.Errorf("maxRetryInterval of %v is lower than minimum value of %v", self.maxRetryInterval, MinRetryInterval)
}
if self.maxRetryInterval > MaxRetryInterval {
return errors.Errorf("maxRetryInterval of %v is larger than maximum value of %v", self.maxRetryInterval, MaxRetryInterval)
}
}
if self.minRetryInterval > self.maxRetryInterval {
return errors.Errorf("minRetryInterval of %v is larger than maxRetryInterval value of %v", self.minRetryInterval, self.maxRetryInterval)
}
return nil
}
type dialerConfig struct {
split bool
localBinding string
groups []string
options *channel.Options
healthyBackoffConfig *backoffConfig
unhealthyBackoffConfig *backoffConfig
}