Skip to content

Commit bd07b4b

Browse files
authored
Refine naming of core slots (#231)
1 parent 8a2f5c4 commit bd07b4b

9 files changed

+32
-164
lines changed

api/slot_chain.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ func GlobalSlotChain() *base.SlotChain {
2727

2828
func BuildDefaultSlotChain() *base.SlotChain {
2929
sc := base.NewSlotChain()
30-
sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{})
31-
sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{})
32-
sc.AddRuleCheckSlotLast(&flow.FlowSlot{})
30+
sc.AddStatPrepareSlotLast(&stat.ResourceNodePrepareSlot{})
31+
sc.AddRuleCheckSlotLast(&system.AdaptiveSlot{})
32+
sc.AddRuleCheckSlotLast(&flow.Slot{})
3333
sc.AddRuleCheckSlotLast(&circuitbreaker.Slot{})
3434
sc.AddRuleCheckSlotLast(&hotspot.Slot{})
35-
sc.AddStatSlotLast(&stat.StatisticSlot{})
36-
sc.AddStatSlotLast(&log.LogSlot{})
35+
sc.AddStatSlotLast(&stat.Slot{})
36+
sc.AddStatSlotLast(&log.Slot{})
3737
sc.AddStatSlotLast(&circuitbreaker.MetricStatSlot{})
3838
sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{})
3939
return sc

core/flow/slot.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88
"github.com/alibaba/sentinel-golang/logging"
99
)
1010

11-
// FlowSlot
12-
type FlowSlot struct {
11+
type Slot struct {
1312
}
1413

15-
func (s *FlowSlot) Check(ctx *base.EntryContext) *base.TokenResult {
14+
func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
1615
res := ctx.Resource.Name()
1716
tcs := getTrafficControllerListFor(res)
1817
result := ctx.RuleCheckResult

core/log/slot.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"github.com/alibaba/sentinel-golang/core/base"
55
)
66

7-
type LogSlot struct {
7+
type Slot struct {
88
}
99

10-
func (s *LogSlot) OnEntryPassed(_ *base.EntryContext) {
10+
func (s *Slot) OnEntryPassed(_ *base.EntryContext) {
1111

1212
}
1313

14-
func (s *LogSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
14+
func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
1515
// TODO: write sentinel-block.log here
1616
}
1717

18-
func (s *LogSlot) OnCompleted(_ *base.EntryContext) {
18+
func (s *Slot) OnCompleted(_ *base.EntryContext) {
1919

2020
}

core/stat/resource_node.go

+3-38
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
package stat
22

33
import (
4-
"fmt"
5-
"sync"
6-
74
"github.com/alibaba/sentinel-golang/core/base"
8-
sbase "github.com/alibaba/sentinel-golang/core/stat/base"
95
)
106

117
type ResourceNode struct {
128
BaseStatNode
139

1410
resourceName string
1511
resourceType base.ResourceType
16-
// key is "sampleCount/intervalInMs"
17-
readOnlyStats map[string]*sbase.SlidingWindowMetric
18-
updateLock sync.RWMutex
1912
}
2013

2114
// NewResourceNode creates a new resource node with given name and classification.
2215
func NewResourceNode(resourceName string, resourceType base.ResourceType) *ResourceNode {
2316
return &ResourceNode{
2417
// TODO: make this configurable
25-
BaseStatNode: *NewBaseStatNode(base.DefaultSampleCount, base.DefaultIntervalMs),
26-
resourceName: resourceName,
27-
resourceType: resourceType,
28-
readOnlyStats: make(map[string]*sbase.SlidingWindowMetric),
18+
BaseStatNode: *NewBaseStatNode(base.DefaultSampleCount, base.DefaultIntervalMs),
19+
resourceName: resourceName,
20+
resourceType: resourceType,
2921
}
3022
}
3123

@@ -36,30 +28,3 @@ func (n *ResourceNode) ResourceType() base.ResourceType {
3628
func (n *ResourceNode) ResourceName() string {
3729
return n.resourceName
3830
}
39-
40-
func (n *ResourceNode) GetSlidingWindowMetric(key string) *sbase.SlidingWindowMetric {
41-
n.updateLock.RLock()
42-
defer n.updateLock.RUnlock()
43-
return n.readOnlyStats[key]
44-
}
45-
46-
func (n *ResourceNode) GetOrCreateSlidingWindowMetric(sampleCount, intervalInMs uint32) *sbase.SlidingWindowMetric {
47-
key := fmt.Sprintf("%d/%d", sampleCount, intervalInMs)
48-
fastVal := n.GetSlidingWindowMetric(key)
49-
if fastVal != nil {
50-
return fastVal
51-
}
52-
53-
n.updateLock.Lock()
54-
defer n.updateLock.Unlock()
55-
56-
v, exist := n.readOnlyStats[key]
57-
if exist {
58-
return v
59-
}
60-
61-
newSlidingWindow := sbase.NewSlidingWindowMetric(sampleCount, intervalInMs, n.arr)
62-
n.readOnlyStats[key] = newSlidingWindow
63-
// TODO clean unused entity in readOnlyStats.
64-
return newSlidingWindow
65-
}

core/stat/resource_node_test.go

-90
This file was deleted.

core/stat/stat_prepare_slot.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"github.com/alibaba/sentinel-golang/core/base"
55
)
66

7-
type StatNodePrepareSlot struct {
7+
type ResourceNodePrepareSlot struct {
88
}
99

10-
func (s *StatNodePrepareSlot) Prepare(ctx *base.EntryContext) {
10+
func (s *ResourceNodePrepareSlot) Prepare(ctx *base.EntryContext) {
1111
node := GetOrCreateResourceNode(ctx.Resource.Name(), ctx.Resource.Classification())
1212
// Set the resource node to the context.
1313
ctx.StatNode = node

core/stat/stat_slot.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,24 @@ import (
55
"github.com/alibaba/sentinel-golang/util"
66
)
77

8-
const SlotName = "StatisticSlot"
9-
10-
type StatisticSlot struct {
11-
}
12-
13-
func (s *StatisticSlot) String() string {
14-
return SlotName
8+
type Slot struct {
159
}
1610

17-
func (s *StatisticSlot) OnEntryPassed(ctx *base.EntryContext) {
11+
func (s *Slot) OnEntryPassed(ctx *base.EntryContext) {
1812
s.recordPassFor(ctx.StatNode, ctx.Input.AcquireCount)
1913
if ctx.Resource.FlowType() == base.Inbound {
2014
s.recordPassFor(InboundNode(), ctx.Input.AcquireCount)
2115
}
2216
}
2317

24-
func (s *StatisticSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
18+
func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
2519
s.recordBlockFor(ctx.StatNode, ctx.Input.AcquireCount)
2620
if ctx.Resource.FlowType() == base.Inbound {
2721
s.recordBlockFor(InboundNode(), ctx.Input.AcquireCount)
2822
}
2923
}
3024

31-
func (s *StatisticSlot) OnCompleted(ctx *base.EntryContext) {
25+
func (s *Slot) OnCompleted(ctx *base.EntryContext) {
3226
rt := util.CurrentTimeMillis() - ctx.StartTime()
3327
ctx.PutRt(rt)
3428
s.recordCompleteFor(ctx.StatNode, ctx.Input.AcquireCount, rt, ctx.Err())
@@ -37,22 +31,22 @@ func (s *StatisticSlot) OnCompleted(ctx *base.EntryContext) {
3731
}
3832
}
3933

40-
func (s *StatisticSlot) recordPassFor(sn base.StatNode, count uint32) {
34+
func (s *Slot) recordPassFor(sn base.StatNode, count uint32) {
4135
if sn == nil {
4236
return
4337
}
4438
sn.IncreaseGoroutineNum()
4539
sn.AddMetric(base.MetricEventPass, uint64(count))
4640
}
4741

48-
func (s *StatisticSlot) recordBlockFor(sn base.StatNode, count uint32) {
42+
func (s *Slot) recordBlockFor(sn base.StatNode, count uint32) {
4943
if sn == nil {
5044
return
5145
}
5246
sn.AddMetric(base.MetricEventBlock, uint64(count))
5347
}
5448

55-
func (s *StatisticSlot) recordCompleteFor(sn base.StatNode, count uint32, rt uint64, err error) {
49+
func (s *Slot) recordCompleteFor(sn base.StatNode, count uint32, rt uint64, err error) {
5650
if sn == nil {
5751
return
5852
}

core/system/slot.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"github.com/alibaba/sentinel-golang/core/stat"
66
)
77

8-
type SystemAdaptiveSlot struct {
8+
type AdaptiveSlot struct {
99
}
1010

11-
func (s *SystemAdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
11+
func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
1212
if ctx == nil || ctx.Resource == nil || ctx.Resource.FlowType() != base.Inbound {
1313
return nil
1414
}
@@ -29,7 +29,7 @@ func (s *SystemAdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
2929
return result
3030
}
3131

32-
func (s *SystemAdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
32+
func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
3333
threshold := rule.TriggerCount
3434
switch rule.MetricType {
3535
case InboundQPS:

core/system/slot_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestCheckNilInput(t *testing.T) {
12-
var sas *SystemAdaptiveSlot
12+
var sas *AdaptiveSlot
1313

1414
t.Run("NilInput", func(t *testing.T) {
1515
r := sas.Check(nil)
@@ -29,7 +29,7 @@ func TestCheckNilInput(t *testing.T) {
2929
}
3030

3131
func TestCheckEmptyRule(t *testing.T) {
32-
var sas *SystemAdaptiveSlot
32+
var sas *AdaptiveSlot
3333
rw := base.NewResourceWrapper("test", base.ResTypeCommon, base.Inbound)
3434
r := sas.Check(&base.EntryContext{
3535
Resource: rw,
@@ -39,7 +39,7 @@ func TestCheckEmptyRule(t *testing.T) {
3939
}
4040

4141
func TestDoCheckRuleConcurrency(t *testing.T) {
42-
var sas *SystemAdaptiveSlot
42+
var sas *AdaptiveSlot
4343
rule := &Rule{MetricType: Concurrency,
4444
TriggerCount: 0.5}
4545

@@ -59,7 +59,7 @@ func TestDoCheckRuleConcurrency(t *testing.T) {
5959
}
6060

6161
func TestDoCheckRuleLoad(t *testing.T) {
62-
var sas *SystemAdaptiveSlot
62+
var sas *AdaptiveSlot
6363
rule := &Rule{MetricType: Load,
6464
TriggerCount: 0.5}
6565

@@ -80,7 +80,7 @@ func TestDoCheckRuleLoad(t *testing.T) {
8080
}
8181

8282
func TestDoCheckRuleCpuUsage(t *testing.T) {
83-
var sas *SystemAdaptiveSlot
83+
var sas *AdaptiveSlot
8484
rule := &Rule{MetricType: CpuUsage,
8585
TriggerCount: 0.5}
8686

@@ -101,7 +101,7 @@ func TestDoCheckRuleCpuUsage(t *testing.T) {
101101
}
102102

103103
func TestDoCheckRuleDefault(t *testing.T) {
104-
var sas *SystemAdaptiveSlot
104+
var sas *AdaptiveSlot
105105
rule := &Rule{MetricType: MetricTypeSize,
106106
TriggerCount: 0.5}
107107

0 commit comments

Comments
 (0)