-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
159 lines (126 loc) · 3.34 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
package runtime
import (
"context"
"errors"
"sync"
"time"
"github.com/MindHunter86/addie/utils"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
)
var (
ErrConfigInvalidStep = errors.New("config storage - softer-step must be >= 0 and < 100")
)
type StorageParam uint8
const (
ParamLottery StorageParam = iota
ParamQuality
ParamBlocklist
ParamBlocklistIps
ParamLimiter
ParamAccessStdout
ParamAccessLevel
paramMaxSize // used only for make(maxvalue)
)
var ParamDefaults = map[StorageParam]interface{}{
ParamLottery: 100,
ParamQuality: utils.TitleQualityHD,
ParamBlocklist: 1,
ParamBlocklistIps: []string{},
ParamLimiter: 0,
ParamAccessStdout: 1,
ParamAccessLevel: zerolog.InfoLevel,
}
var GetNameByParam = map[StorageParam]string{
ParamLottery: runtimeChangesHumanize[RuntimePatchLottery],
ParamQuality: runtimeChangesHumanize[RuntimePatchQuality],
ParamBlocklist: runtimeChangesHumanize[RuntimePatchBlocklist],
ParamBlocklistIps: runtimeChangesHumanize[RuntimePatchBlocklistIps],
ParamLimiter: runtimeChangesHumanize[RuntimePatchLimiter],
ParamAccessStdout: runtimeChangesHumanize[RuntimePatchAccessStdout],
ParamAccessLevel: runtimeChangesHumanize[RuntimePatchAccessLevel],
}
type Storage struct {
mu sync.RWMutex
st map[StorageParam]*Entry
}
var (
done func() <-chan struct{}
deployStep int
deployInteration time.Duration
)
func NewStorage(c context.Context) (st *Storage, _ error) {
done = c.Done
ccx := c.Value(utils.ContextKeyCliContext).(*cli.Context)
deployStep, deployInteration =
ccx.Int("balancer-softer-step"),
ccx.Duration("balancer-softer-tick")
if deployStep < 0 || deployStep > 99 {
return nil, ErrConfigInvalidStep
}
if deployInteration < 10*time.Second {
log.Warn().Msg("low value detected for softer-tick arg")
}
st = &Storage{
st: make(map[StorageParam]*Entry, paramMaxSize),
}
st.loadDefaults()
return
}
func (m *Storage) loadDefaults() {
m.mu.Lock()
defer m.mu.Unlock()
for param, val := range ParamDefaults {
e := newEntry(val)
m.st[param] = e
log.Trace().Msgf("loaded default value for %s - %+v", GetNameByParam[param], val)
}
}
func (m *Storage) getEntry(param StorageParam) (e *Entry, ok bool) {
m.mu.RLock()
defer m.mu.RUnlock()
e, ok = m.st[param]
return e, ok && e != nil
}
func (m *Storage) setEntryByValue(param StorageParam, val interface{}) (e *Entry) {
m.mu.Lock()
defer m.mu.Unlock()
if val == nil {
val = ParamDefaults[param]
}
e = newEntry(val)
m.st[param] = e
log.Trace().Msgf("config param %s updated with value %+v", GetNameByParam[param], val)
return
}
func (m *Storage) Get(param StorageParam) interface{} {
if e, ok := m.getEntry(param); ok {
return e.get()
}
// TODO: remove this
panic("undefined entry or param not found in config storage")
}
func (m *Storage) Set(param StorageParam, val interface{}) {
e, ok := m.getEntry(param)
if !ok && e != nil {
panic("value is not nil but param not found in config storage")
} else if !ok {
m.setEntryByValue(param, val)
return
}
e.set(val)
}
func (m *Storage) SetSmoothly(param StorageParam, val interface{}) {
e, ok := m.getEntry(param)
if !ok && e != nil {
panic("value is not nil but param not found in config storage")
} else if !ok {
m.setEntryByValue(param, val)
return
}
if deployStep == 0 {
e.set(val)
return
}
go e.deploy(val)
}