-
Notifications
You must be signed in to change notification settings - Fork 13
/
feature_flag_mysql.go
222 lines (176 loc) · 6.07 KB
/
feature_flag_mysql.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
package featureflag
import (
"errors"
"fmt"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service/component/app"
"github.com/coretrix/hitrix/service/component/clock"
errorlogger "github.com/coretrix/hitrix/service/component/error_logger"
)
type IFeatureFlag interface {
GetName() string
ScriptsSingleInstance() []app.IScript
ScriptsMultiInstance() []app.IScript
}
type serviceFeatureFlag struct {
featureFlags map[string]IFeatureFlag
errorLoggerService errorlogger.ErrorLogger
}
func NewFeatureFlagService(errorLoggerService errorlogger.ErrorLogger) ServiceFeatureFlagInterface {
featureFlags := make(map[string]IFeatureFlag)
return &serviceFeatureFlag{
featureFlags: featureFlags,
errorLoggerService: errorLoggerService,
}
}
func (s *serviceFeatureFlag) IsActive(ormService *beeorm.Engine, name string) bool {
if name == "" {
panic("name cannot be empty")
}
query := beeorm.NewRedisSearchQuery()
query.FilterString("Name", name)
featureFlagEntity := &entity.FeatureFlagEntity{}
found := ormService.RedisSearchOne(featureFlagEntity, query)
if !found {
return false
}
return featureFlagEntity.Enabled && featureFlagEntity.Registered
}
func (s *serviceFeatureFlag) FailIfIsNotActive(ormService *beeorm.Engine, name string) error {
isActive := s.IsActive(ormService, name)
if !isActive {
return fmt.Errorf("feature (%s) is not active", name)
}
return nil
}
func (s *serviceFeatureFlag) Enable(ormService *beeorm.Engine, name string) error {
if name == "" {
panic("name cannot be empty")
}
query := beeorm.NewRedisSearchQuery()
query.FilterString("Name", name)
featureFlagEntity := &entity.FeatureFlagEntity{}
found := ormService.RedisSearchOne(featureFlagEntity, query)
if !found {
return errors.New("feature cannot be found")
}
featureFlagEntity.Enabled = true
ormService.Flush(featureFlagEntity)
return nil
}
func (s *serviceFeatureFlag) Disable(ormService *beeorm.Engine, name string) error {
if name == "" {
panic("name cannot be empty")
}
query := beeorm.NewRedisSearchQuery()
query.FilterString("Name", name)
featureFlagEntity := &entity.FeatureFlagEntity{}
found := ormService.RedisSearchOne(featureFlagEntity, query)
if !found {
return errors.New("feature cannot be found")
}
featureFlagEntity.Enabled = false
ormService.Flush(featureFlagEntity)
return nil
}
func (s *serviceFeatureFlag) getAllActive(ormService *beeorm.Engine, pager *beeorm.Pager) []IFeatureFlag {
query := beeorm.NewRedisSearchQuery()
query.FilterBool("Registered", true)
query.FilterBool("Enabled", true)
var featureFlagEntities []*entity.FeatureFlagEntity
ormService.RedisSearch(&featureFlagEntities, query, pager)
activeFeatureFlags := make([]IFeatureFlag, 0)
for _, featureFlagEntity := range featureFlagEntities {
if _, ok := s.featureFlags[featureFlagEntity.Name]; !ok {
s.errorLoggerService.LogError("feature flag " + featureFlagEntity.Name + " is not registered")
continue
}
activeFeatureFlags = append(activeFeatureFlags, s.featureFlags[featureFlagEntity.Name])
}
return activeFeatureFlags
}
func (s *serviceFeatureFlag) GetScriptsSingleInstance(ormService *beeorm.Engine) []app.IScript {
activeFeatureFlags := s.getAllActive(ormService, beeorm.NewPager(1, 1000))
allScripts := make([]app.IScript, 0)
for _, featureFlag := range activeFeatureFlags {
allScripts = append(allScripts, featureFlag.ScriptsSingleInstance()...)
}
return allScripts
}
func (s *serviceFeatureFlag) GetScriptsMultiInstance(ormService *beeorm.Engine) []app.IScript {
activeFeatureFlags := s.getAllActive(ormService, beeorm.NewPager(1, 1000))
allScripts := make([]app.IScript, 0)
for _, featureFlag := range activeFeatureFlags {
allScripts = append(allScripts, featureFlag.ScriptsMultiInstance()...)
}
return allScripts
}
func (s *serviceFeatureFlag) Register(featureFlags ...IFeatureFlag) {
s.featureFlags = make(map[string]IFeatureFlag)
for _, featureFlag := range featureFlags {
if _, has := s.featureFlags[featureFlag.GetName()]; has {
panic("feature flag with name '" + featureFlag.GetName() + "' already exists")
}
s.featureFlags[featureFlag.GetName()] = featureFlag
}
}
func (s *serviceFeatureFlag) Sync(ormService *beeorm.Engine, clockService clock.IClock) {
var featureFlagEntities []*entity.FeatureFlagEntity
var lastID uint64
for {
var rows []*entity.FeatureFlagEntity
pager := beeorm.NewPager(1, 1000)
ormService.Search(beeorm.NewWhere("ID > ? ORDER BY ID ASC", lastID), pager, &rows)
if len(rows) == 0 {
break
}
lastID = rows[len(rows)-1].ID
featureFlagEntities = append(featureFlagEntities, rows...)
if len(rows) < pager.PageSize {
break
}
}
flusher := ormService.NewFlusher()
dbFeatureFlags := make(map[string]*entity.FeatureFlagEntity)
for _, featureFlagEntity := range featureFlagEntities {
if featureFlagEntity != nil {
dbFeatureFlags[featureFlagEntity.Name] = featureFlagEntity
} else {
s.errorLoggerService.LogError("feature flag is nil")
}
}
for _, registeredFeatureFlag := range s.featureFlags {
if _, ok := dbFeatureFlags[registeredFeatureFlag.GetName()]; !ok {
featureFlagEntity := &entity.FeatureFlagEntity{
Name: registeredFeatureFlag.GetName(),
Registered: true,
Enabled: false,
UpdatedAt: nil,
CreatedAt: clockService.Now(),
}
err := ormService.FlushWithCheck(featureFlagEntity)
if err != nil {
if duplicateKeyError, ok := err.(*beeorm.DuplicatedKeyError); ok {
if duplicateKeyError.Index != "Name" {
panic(err)
}
} else {
panic(err)
}
}
} else if !dbFeatureFlags[registeredFeatureFlag.GetName()].Registered {
dbFeatureFlags[registeredFeatureFlag.GetName()].Registered = true
dbFeatureFlags[registeredFeatureFlag.GetName()].UpdatedAt = clockService.NowPointer()
flusher.Track(dbFeatureFlags[registeredFeatureFlag.GetName()])
}
}
for name, dbFeatureFlag := range dbFeatureFlags {
if _, ok := s.featureFlags[name]; !ok {
dbFeatureFlag.Registered = false
dbFeatureFlag.UpdatedAt = clockService.NowPointer()
flusher.Track(dbFeatureFlag)
}
}
flusher.Flush()
}