-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.go
More file actions
74 lines (64 loc) · 2.25 KB
/
engine.go
File metadata and controls
74 lines (64 loc) · 2.25 KB
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
package engine
import (
"fmt"
"math"
"github.com/splitio/go-split-commons/v6/engine/evaluator/impressionlabels"
"github.com/splitio/go-split-commons/v6/engine/grammar"
"github.com/splitio/go-split-commons/v6/engine/hash"
"github.com/splitio/go-toolkit/v5/hasher"
"github.com/splitio/go-toolkit/v5/injection"
"github.com/splitio/go-toolkit/v5/logging"
)
// Engine struct is responsible for checking if any of the conditions of the feature flag matches,
// performing traffic allocation, calculating the bucket and returning the appropriate treatment
type Engine struct {
logger logging.LoggerInterface
}
// DoEvaluation performs the main evaluation against each condition
func (e *Engine) DoEvaluation(
split *grammar.Split,
key string,
bucketingKey string,
attributes map[string]interface{},
ctx *injection.Context,
) (string, string) {
inRollOut := false
for _, condition := range split.Conditions() {
if !inRollOut && condition.ConditionType() == grammar.ConditionTypeRollout {
if split.TrafficAllocation() < 100 {
bucket := e.calculateBucket(split.Algo(), bucketingKey, split.TrafficAllocationSeed())
if bucket > split.TrafficAllocation() {
e.logger.Debug(fmt.Sprintf(
"Traffic allocation exceeded for feature %s and key %s."+
" Returning default treatment", split.Name(), key,
))
defaultTreatment := split.DefaultTreatment()
return defaultTreatment, impressionlabels.NotInSplit
}
inRollOut = true
}
}
if condition.Matches(key, &bucketingKey, attributes, ctx) {
bucket := e.calculateBucket(split.Algo(), bucketingKey, split.Seed())
treatment := condition.CalculateTreatment(bucket)
return treatment, condition.Label()
}
}
return "", impressionlabels.NoConditionMatched
}
func (e *Engine) calculateBucket(algo int, bucketingKey string, seed int64) int {
var hashedKey uint32
switch algo {
case grammar.SplitAlgoMurmur:
hashedKey = hasher.Sum32WithSeed([]byte(bucketingKey), uint32(seed))
case grammar.SplitAlgoLegacy:
fallthrough
default:
hashedKey = hash.Legacy([]byte(bucketingKey), uint32(seed))
}
return int(math.Abs(float64(hashedKey%100)) + 1)
}
// NewEngine instantiates and returns a new engine
func NewEngine(logger logging.LoggerInterface) *Engine {
return &Engine{logger: logger}
}