-
Notifications
You must be signed in to change notification settings - Fork 53
/
flexible_rollout.go
67 lines (54 loc) · 1.55 KB
/
flexible_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
package strategies
import (
"github.com/Unleash/unleash-client-go/v4/context"
"github.com/Unleash/unleash-client-go/v4/strategy"
)
type stickiness string
const (
defaultStickiness stickiness = "default"
randomStickiness stickiness = "random"
)
type flexibleRolloutStrategy struct {
random *rng
}
// NewFlexibleRolloutStrategy creates a new instance of the flexible rollout strategy.
func NewFlexibleRolloutStrategy() *flexibleRolloutStrategy {
s := &flexibleRolloutStrategy{
random: newRng(),
}
return s
}
func (s flexibleRolloutStrategy) Name() string {
return "flexibleRollout"
}
func (s flexibleRolloutStrategy) resolveStickiness(st stickiness, ctx context.Context) string {
switch st {
case defaultStickiness:
return coalesce(ctx.UserId, ctx.SessionId, s.random.string())
case randomStickiness:
return s.random.string()
default:
return ctx.Field(string(st))
}
}
func (s flexibleRolloutStrategy) IsEnabled(params map[string]interface{}, ctx *context.Context) bool {
groupID := ""
if gID, found := params[strategy.ParamGroupId]; found {
groupID = gID.(string)
}
rollout, found := params[strategy.ParamRollout]
if !found {
return false
}
percentage, ok := parameterAsFloat64(rollout)
if !ok {
return false
}
sticky := coalesce(params[strategy.ParamStickiness].(string), string(defaultStickiness))
stickinessID := s.resolveStickiness(stickiness(sticky), *ctx)
if stickinessID == "" {
return false
}
normalizedID := normalizedRolloutValue(stickinessID, groupID)
return percentage > 0 && float64(normalizedID) <= percentage
}