-
Notifications
You must be signed in to change notification settings - Fork 351
/
traffic_segment.go
65 lines (53 loc) · 1.53 KB
/
traffic_segment.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
package kubernetes
import (
"github.com/zalando/skipper/dataclients/kubernetes/definitions"
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/predicates"
)
type trafficSegmentPredicate struct {
min, max float64
}
func trafficSegmentPredicateCalculator[T definitions.WeightedBackend](b []T) map[string]backendTraffic {
bt := make(map[string]backendTraffic, len(b))
sum := 0.0
for _, bi := range b {
if _, ok := bt[bi.GetName()].(*trafficSegmentPredicate); ok {
// ignore duplicate backends
continue
}
p := &trafficSegmentPredicate{}
bt[bi.GetName()] = p
p.min = sum
sum += bi.GetWeight()
p.max = sum
}
if sum == 0 {
// evenly split traffic between backends
// range over b instead of bt for stable order
for _, bi := range b {
p := bt[bi.GetName()].(*trafficSegmentPredicate)
p.min = sum
sum += 1
p.max = sum
}
}
// normalize segments
for _, v := range bt {
p := v.(*trafficSegmentPredicate)
p.min /= sum
// last segment always ends up with p.max equal to one because
// dividing a finite non-zero value by itself always produces one,
// see https://stackoverflow.com/questions/63439390/does-ieee-754-float-division-or-subtraction-by-itself-always-result-in-the-same
p.max /= sum
}
return bt
}
func (ts *trafficSegmentPredicate) allowed() bool {
return ts.min != ts.max
}
func (ts *trafficSegmentPredicate) apply(r *eskip.Route) {
if ts.min == 0 && ts.max == 1 {
return
}
r.Predicates = appendPredicate(r.Predicates, predicates.TrafficSegmentName, ts.min, ts.max)
}