Skip to content

Commit f33b480

Browse files
committed
chore:rule.Equals add ParamKey check;
chore:ParamKey add supplementary explanation chore:adjust the order of extraction parameters, give priority to paramkey, and avoid the problem of paramIndex default value 0;update test unit
1 parent b244a55 commit f33b480

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

core/hotspot/rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type Rule struct {
8282
ParamIndex int `json:"paramIndex"`
8383
// ParamKey is the key in context attachments map.
8484
// ParamKey can be used as a supplement to ParamIndex to facilitate rules to quickly obtain parameter from a large number of parameters
85-
// Mutually exclusive with ParamIndex
85+
// Mutually exclusive with ParamIndex,The extraction priority is higher than ParamIndex to avoid the problem of ParamIndex default value 0
8686
ParamKey string `json:"paramKey"`
8787
// Threshold is the threshold to trigger rejection
8888
Threshold int64 `json:"threshold"`
@@ -120,7 +120,7 @@ func (r *Rule) IsStatReusable(newRule *Rule) bool {
120120

121121
// Equals checks whether current rule is consistent with the given rule.
122122
func (r *Rule) Equals(newRule *Rule) bool {
123-
baseCheck := r.Resource == newRule.Resource && r.MetricType == newRule.MetricType && r.ControlBehavior == newRule.ControlBehavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.ParamIndex == newRule.ParamIndex && r.Threshold == newRule.Threshold && r.DurationInSec == newRule.DurationInSec && reflect.DeepEqual(r.SpecificItems, newRule.SpecificItems)
123+
baseCheck := r.Resource == newRule.Resource && r.MetricType == newRule.MetricType && r.ControlBehavior == newRule.ControlBehavior && r.ParamsMaxCapacity == newRule.ParamsMaxCapacity && r.ParamIndex == newRule.ParamIndex && r.ParamKey == newRule.ParamKey && r.Threshold == newRule.Threshold && r.DurationInSec == newRule.DurationInSec && reflect.DeepEqual(r.SpecificItems, newRule.SpecificItems)
124124
if !baseCheck {
125125
return false
126126
}

core/hotspot/rule_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func Test_Rule_Equals(t *testing.T) {
6262
MetricType: Concurrency,
6363
ControlBehavior: Reject,
6464
ParamIndex: 0,
65+
ParamKey: "testKey",
6566
Threshold: 110.0,
6667
MaxQueueingTimeMs: 5,
6768
BurstCount: 10,
@@ -79,6 +80,7 @@ func Test_Rule_Equals(t *testing.T) {
7980
MetricType: Concurrency,
8081
ControlBehavior: Reject,
8182
ParamIndex: 0,
83+
ParamKey: "testKey",
8284
Threshold: 110.0,
8385
MaxQueueingTimeMs: 5,
8486
BurstCount: 10,

core/hotspot/traffic_shaping.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ func (c *baseTrafficShapingController) ExtractArgs(ctx *base.EntryContext) (valu
165165
if c == nil {
166166
return nil
167167
}
168-
value = c.extractArgs(ctx)
168+
value = c.extractAttachmentArgs(ctx)
169169
if value != nil {
170170
return
171171
}
172-
value = c.extractAttachmentArgs(ctx)
172+
value = c.extractArgs(ctx)
173173
if value != nil {
174174
return
175175
}

core/hotspot/traffic_shaping_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,20 +345,21 @@ func Test_baseTrafficShapingController_ExtractArgs(t *testing.T) {
345345
// set data
346346
args[0] = 1
347347
args[1] = 2
348-
attachments["test1"] = "v1"
348+
value1 := "v1"
349+
attachments["test1"] = value1
349350

350351
// set index or key
351352
// exist
352353
c.paramIndex = 0
353354
c.paramKey = "test1"
354355
ret = c.ExtractArgs(ctx)
355-
assert.True(t, reflect.DeepEqual(ret, 1), ret)
356+
assert.True(t, reflect.DeepEqual(ret, value1), ret)
356357

357358
// part exist 1
358359
c.paramIndex = 10
359360
c.paramKey = "test1"
360361
ret = c.ExtractArgs(ctx)
361-
assert.True(t, reflect.DeepEqual(ret, "v1"), ret)
362+
assert.True(t, reflect.DeepEqual(ret, value1), ret)
362363

363364
// part exist 2
364365
c.paramIndex = 1

0 commit comments

Comments
 (0)