-
Notifications
You must be signed in to change notification settings - Fork 135
/
cost_visitor.go
60 lines (53 loc) · 1.92 KB
/
cost_visitor.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
package xt_common
import (
"github.com/openziti/ziti/controller/xt"
"math"
)
type CostVisitor struct {
FailureCosts xt.FailureCosts
CircuitCost uint16
}
func (visitor *CostVisitor) VisitDialFailed(event xt.TerminatorEvent) {
change := visitor.FailureCosts.Failure(event.GetTerminator().GetId())
if change > 0 {
xt.GlobalCosts().UpdateDynamicCost(event.GetTerminator().GetId(), func(cost uint16) uint16 {
if cost < (math.MaxUint16 - change) {
return cost + change
}
return math.MaxUint16
})
}
}
func (visitor *CostVisitor) VisitDialSucceeded(event xt.TerminatorEvent) {
credit := visitor.FailureCosts.Success(event.GetTerminator().GetId())
if credit != visitor.CircuitCost {
xt.GlobalCosts().UpdateDynamicCost(event.GetTerminator().GetId(), func(cost uint16) uint16 {
if visitor.CircuitCost > credit {
increase := visitor.CircuitCost - credit
if cost < (math.MaxUint16 - increase) {
// pfxlog.Logger().Infof("%v: dial+ %v -> %v", event.GetTerminator().GetId(), cost, cost+increase)
return cost + increase
}
// pfxlog.Logger().Infof("%v: dial+ %v -> %v", event.GetTerminator().GetId(), cost, math.MaxUint16)
return math.MaxUint16
}
decrease := credit - visitor.CircuitCost
if decrease > cost {
// pfxlog.Logger().Infof("%v: dial+ %v -> %v", event.GetTerminator().GetId(), cost, 0)
return 0
}
// pfxlog.Logger().Infof("%v: dial+ %v -> %v", event.GetTerminator().GetId(), cost, cost-decrease)
return cost - decrease
})
}
}
func (visitor *CostVisitor) VisitCircuitRemoved(event xt.TerminatorEvent) {
xt.GlobalCosts().UpdateDynamicCost(event.GetTerminator().GetId(), func(cost uint16) uint16 {
if cost > visitor.CircuitCost {
// pfxlog.Logger().Infof("%v: sess- %v -> %v", event.GetTerminator().GetId(), cost, cost-1)
return cost - visitor.CircuitCost
}
// pfxlog.Logger().Infof("%v: sess- %v -> %v", event.GetTerminator().GetId(), cost, 0)
return 0
})
}