-
Notifications
You must be signed in to change notification settings - Fork 50
/
settings.go
72 lines (58 loc) · 1.65 KB
/
settings.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
package exec
import (
"fmt"
"time"
"github.com/justtrackio/gosoline/pkg/cfg"
)
type BackoffSettings struct {
CancelDelay time.Duration `cfg:"cancel_delay"`
InitialInterval time.Duration `cfg:"initial_interval" default:"50ms"`
MaxAttempts int `cfg:"max_attempts" default:"10"`
MaxElapsedTime time.Duration `cfg:"max_elapsed_time" default:"10m"`
MaxInterval time.Duration `cfg:"max_interval" default:"10s"`
}
func ReadBackoffSettings(config cfg.Config, paths ...string) BackoffSettings {
typ := "default"
paths = append(paths, "exec")
for i := len(paths) - 1; i >= 0; i-- {
key := fmt.Sprintf("%s.backoff", paths[i])
keyType := fmt.Sprintf("%s.backoff.type", paths[i])
if !config.IsSet(key) {
continue
}
if !config.IsSet(keyType) {
typ = "custom"
continue
}
typ = config.GetString(keyType)
}
if settings, ok := predefined[typ]; ok {
return settings
}
additionalDefaults := make([]cfg.UnmarshalDefaults, 0)
for i := 1; i < len(paths); i++ {
key := fmt.Sprintf("%s.backoff", paths[i])
additionalDefaults = append(additionalDefaults, cfg.UnmarshalWithDefaultsFromKey(key, "."))
}
key := fmt.Sprintf("%s.backoff", paths[0])
settings := &BackoffSettings{}
config.UnmarshalKey(key, settings, additionalDefaults...)
return *settings
}
var predefined = map[string]BackoffSettings{
"api": {
InitialInterval: time.Millisecond * 100,
MaxElapsedTime: time.Second * 10,
MaxInterval: time.Second,
},
"once": {
MaxAttempts: 1,
MaxElapsedTime: 0,
},
"infinite": {
InitialInterval: time.Millisecond * 50,
MaxAttempts: 0,
MaxElapsedTime: 0,
MaxInterval: time.Second * 10,
},
}