/
slot.go
76 lines (71 loc) · 1.84 KB
/
slot.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
package system
import (
"github.com/brucewangzhihua/sentinel-golang/core/base"
"github.com/brucewangzhihua/sentinel-golang/core/stat"
)
type AdaptiveSlot struct {
}
func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
if ctx == nil || ctx.Resource == nil || ctx.Resource.FlowType() != base.Inbound {
return nil
}
rules := GetRules()
result := ctx.RuleCheckResult
for _, rule := range rules {
passed, snapshotValue := s.doCheckRule(rule)
if passed {
continue
}
if result == nil {
result = base.NewTokenResultBlockedWithCause(base.BlockTypeSystemFlow, rule.MetricType.String(), rule, snapshotValue)
} else {
result.ResetToBlockedWithCause(base.BlockTypeSystemFlow, rule.MetricType.String(), rule, snapshotValue)
}
return result
}
return result
}
func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
threshold := rule.TriggerCount
switch rule.MetricType {
case InboundQPS:
qps := stat.InboundNode().GetQPS(base.MetricEventPass)
res := qps < threshold
return res, qps
case Concurrency:
n := float64(stat.InboundNode().CurrentGoroutineNum())
res := n < threshold
return res, n
case AvgRT:
rt := stat.InboundNode().AvgRT()
res := rt < threshold
return res, rt
case Load:
l := CurrentLoad()
if l > threshold {
if rule.Strategy != BBR || !checkBbrSimple() {
return false, l
}
}
return true, l
case CpuUsage:
c := CurrentCpuUsage()
if c > threshold {
if rule.Strategy != BBR || !checkBbrSimple() {
return false, c
}
}
return true, c
default:
return true, 0
}
}
func checkBbrSimple() bool {
concurrency := stat.InboundNode().CurrentGoroutineNum()
minRt := stat.InboundNode().MinRT()
maxComplete := stat.InboundNode().GetMaxAvg(base.MetricEventComplete)
if concurrency > 1 && float64(concurrency) > maxComplete*minRt/1000 {
return false
}
return true
}