This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollout.go
197 lines (173 loc) · 4.24 KB
/
rollout.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
package rollout
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"log"
"github.com/samuel/go-zookeeper/zk"
)
type Client interface {
Start() error
Stop()
RawPercentage(feature string) (float64, error)
FeatureActive(feature string, userId int64, userGroups []string) bool
}
// is called when a ZK error is encountered
type errorHandlerFunc func(err error)
type client struct {
sync.RWMutex
zk *zk.Conn
currentData map[string]string
stop chan bool
done chan bool
path string
errorHandler errorHandlerFunc
}
func NewClient(zk *zk.Conn, path string, errorHandler errorHandlerFunc) Client {
return &client{
zk: zk,
path: path,
currentData: make(map[string]string),
stop: make(chan bool),
done: make(chan bool),
errorHandler: errorHandler,
}
}
func (r *client) Start() error {
rolloutLog.Info("Starting Rollout service on ", r.path)
exists, _, err := r.zk.Exists(r.path)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Rollout path (%s) does not exist", r.path)
}
go r.poll(r.path)
return nil
}
func (r *client) Stop() {
r.stop <- true
<-r.done
}
func (r *client) poll(path string) {
defer func() { r.done <- true }()
defer rolloutLog.Info("rollout poller shutdown")
for {
data, _, watch, err := r.zk.GetW(path)
if err != nil {
rolloutLog.Error("rollout failed to set watch: ", err)
if r.errorHandler != nil {
r.errorHandler(err)
}
select {
case <-time.After(time.Second):
case <-r.stop:
return
}
continue
}
if err := r.swapData(data); err != nil {
rolloutLog.Error("rollout couldn't unmarshal zookeeper data: ", err)
// re-get the watch so we know when/if the bad data changes
_, _, watch, err = r.zk.GetW(path)
if err != nil {
log.Fatal("could not re-establish a watch after unmarshalling error. Nothing to do but exit")
}
}
select {
case <-watch:
// block until data changes
case <-r.stop:
return
}
}
}
func (r *client) swapData(data []byte) error {
newMap := make(map[string]string)
if err := json.Unmarshal(data, &newMap); err != nil {
return err
}
r.Lock()
defer r.Unlock()
r.currentData = newMap
return nil
}
// ErrFeatureNotFound can be detected to inform better defaulting behaviors
var ErrFeatureNotFound = errors.New("feature not found")
// RawPercentage returns the raw percentage from the rollout section
func (r *client) RawPercentage(feature string) (float64, error) {
feature = "feature:" + feature
r.RLock()
value, ok := r.currentData[feature]
r.RUnlock()
if !ok {
return 0.0, ErrFeatureNotFound
}
splitResult := strings.Split(value, "|")
if len(splitResult) != 3 {
return 0.0, fmt.Errorf("invalid value for %s: %s", feature, value)
}
percentageFloat, err := strconv.ParseFloat(splitResult[0], 64)
if err != nil {
return 0.0, fmt.Errorf("rollout invalid percentage: %v", splitResult[0])
}
return percentageFloat, nil
}
func (r *client) FeatureActive(feature string, userId int64, userGroups []string) bool {
feature = "feature:" + feature
r.RLock()
value, ok := r.currentData[feature]
r.RUnlock()
if !ok {
return false
}
splitResult := strings.Split(value, "|")
if len(splitResult) != 3 {
rolloutLog.Errorf("Rollout: invalid value for %s: %s", feature, value)
return false
}
featureGroups := strings.Split(splitResult[2], ",")
// Short-circuit for pseudo-group "all"
if contains("all", featureGroups) {
return true
}
percentageFloat, err := strconv.ParseFloat(splitResult[0], 64)
if err != nil {
rolloutLog.Error("rollout invalid percentage: ", splitResult[0])
return false
}
percentage := int(percentageFloat)
// Short-circuit for 100%
if percentage == 100 {
return true
}
// Check user ID
userIds := strings.Split(splitResult[1], ",")
userIdString := strconv.FormatInt(userId, 10)
if contains(userIdString, userIds) {
return true
}
// Next, check percentage
if userId%100 < int64(percentage) {
return true
}
// Lastly, check groups
for _, userGroup := range userGroups {
if contains(userGroup, featureGroups) {
return true
}
}
return false
}
func contains(needle string, haystack []string) bool {
for _, i := range haystack {
if i == needle {
return true
}
}
return false
}