-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
292 lines (235 loc) · 6.24 KB
/
manager.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
package rollout
import (
"fmt"
redis "github.com/go-redis/redis/v7"
"github.com/vmihailenco/msgpack/v4"
)
// Manager persists and fetches feature toggles to/from redis
type Manager struct {
client redis.Cmdable
keyPrefix string
randomizePercentage bool
}
// NewManager constructs a new Manager instance
func NewManager(client redis.Cmdable, keyPrefix string, randomizePercentage bool) *Manager {
// nothing is retrieved from redis at this point
// everything is fetched on demand
return &Manager{
client: client,
keyPrefix: keyPrefix,
randomizePercentage: randomizePercentage,
}
}
func (m *Manager) keyName(feature *Feature) string {
return m.keyPrefix + ":" + feature.Name()
}
// get updates the Feature to align with the current value in redis
func (m *Manager) get(feature *Feature) error {
// retrieve feature from redis
data, err := m.client.Get(m.keyName(feature)).Bytes()
if err != nil {
if err == redis.Nil {
// feature isn't in redis, so should be inactive
feature.Lock()
feature.deactivate()
feature.Unlock()
return nil
}
return err
}
feature.Lock()
defer feature.Unlock()
if err := msgpack.Unmarshal(data, feature); err != nil {
return err
}
return nil
}
// Activate globally activates the feature
func (m *Manager) Activate(feature *Feature) error {
if err := m.get(feature); err != nil {
return err
}
feature.Lock()
feature.activate()
data, err := msgpack.Marshal(feature)
if err != nil {
return err
}
feature.Unlock()
if err := m.client.Set(m.keyName(feature), data, 0).Err(); err != nil {
return err
}
return nil
}
// Deactivate globally deactivates the feature
func (m *Manager) Deactivate(feature *Feature) error {
if err := m.get(feature); err != nil {
return err
}
feature.Lock()
feature.deactivate()
data, err := msgpack.Marshal(feature)
if err != nil {
return err
}
feature.Unlock()
if err := m.client.Set(m.keyName(feature), data, 0).Err(); err != nil {
return err
}
return nil
}
// ActivatePercentage activates the feature for a percentage of teams
func (m *Manager) ActivatePercentage(feature *Feature, percentage uint8) error {
if err := m.get(feature); err != nil {
return err
}
feature.Lock()
feature.activatePercentage(percentage)
data, err := msgpack.Marshal(feature)
if err != nil {
return err
}
feature.Unlock()
if err := m.client.Set(m.keyName(feature), data, 0).Err(); err != nil {
return err
}
return nil
}
// IsActive returns whether the given feature is globally active
func (m *Manager) IsActive(feature *Feature) (bool, error) {
// retrieve feature from redis
data, err := m.client.Get(m.keyName(feature)).Bytes()
if err != nil {
if err == redis.Nil {
return false, nil
}
return false, err
}
feature.Lock()
defer feature.Unlock()
if err := msgpack.Unmarshal(data, feature); err != nil {
return false, err
}
return feature.isActive(), nil
}
// IsActiveMulti returns whether the given features are globally active
func (m *Manager) IsActiveMulti(features ...*Feature) ([]bool, error) {
if len(features) == 0 {
return nil, nil
}
featureNames := make([]string, len(features))
for i, feature := range features {
featureNames[i] = m.keyName(feature)
}
// retrieve features from redis
val, err := m.client.MGet(featureNames...).Result()
if err != nil {
return nil, err
}
for _, feature := range features {
feature.Lock()
defer feature.Unlock()
}
results := make([]bool, len(features))
for i, v := range val {
switch t := v.(type) {
case nil:
// feature wasn't found in redis, so considered inactive globally
features[i].deactivate()
case string:
if err := msgpack.Unmarshal([]byte(t), features[i]); err != nil {
return nil, err
}
results[i] = features[i].isActive()
default:
return nil, fmt.Errorf("unexpected type (%T) for msgpack value: %v", v, v)
}
}
return results, nil
}
// ActivateTeam activates the feature for specific team
func (m *Manager) ActivateTeam(teamID int64, feature *Feature) error {
if err := m.get(feature); err != nil {
return err
}
feature.Lock()
feature.activateTeam(teamID)
data, err := msgpack.Marshal(feature)
if err != nil {
return err
}
feature.Unlock()
if err := m.client.Set(m.keyName(feature), data, 0).Err(); err != nil {
return err
}
return nil
}
// DeactivateTeam deactivates the feature for specific team
func (m *Manager) DeactivateTeam(teamID int64, feature *Feature) error {
if err := m.get(feature); err != nil {
return err
}
feature.Lock()
feature.deactivateTeam(teamID)
data, err := msgpack.Marshal(feature)
if err != nil {
return err
}
feature.Unlock()
if err := m.client.Set(m.keyName(feature), data, 0).Err(); err != nil {
return err
}
return nil
}
// IsTeamActive returns whether the given feature is active for a team
func (m *Manager) IsTeamActive(teamID int64, feature *Feature) (bool, error) {
// retrieve feature from redis
data, err := m.client.Get(m.keyName(feature)).Bytes()
if err != nil {
if err == redis.Nil {
return false, nil
}
return false, err
}
feature.Lock()
defer feature.Unlock()
if err := msgpack.Unmarshal(data, feature); err != nil {
return false, err
}
return feature.isTeamActive(teamID, m.randomizePercentage), nil
}
// IsTeamActiveMulti returns whether the given features are globally active
func (m *Manager) IsTeamActiveMulti(teamID int64, features ...*Feature) ([]bool, error) {
if len(features) == 0 {
return nil, nil
}
featureNames := make([]string, len(features))
for i, feature := range features {
featureNames[i] = m.keyName(feature)
}
// retrieve features from redis
val, err := m.client.MGet(featureNames...).Result()
if err != nil {
return nil, err
}
for _, feature := range features {
feature.Lock()
defer feature.Unlock()
}
results := make([]bool, len(features))
for i, v := range val {
switch t := v.(type) {
case nil:
// feature wasn't found in redis, so considered inactive globally
features[i].deactivate()
case string:
if err := msgpack.Unmarshal([]byte(t), features[i]); err != nil {
return nil, err
}
results[i] = features[i].isTeamActive(teamID, m.randomizePercentage)
default:
return nil, fmt.Errorf("unexpected type (%T) for msgpack value: %v", v, v)
}
}
return results, nil
}