Skip to content

Commit 2c99557

Browse files
authored
fix:限流规则disable查询条件失效 (polarismesh#1060)
1 parent e7ce9b9 commit 2c99557

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

common/utils/funcs.go

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func CollectMapKeys(filters map[string]string) []string {
4444
fields := make([]string, 0, len(filters))
4545
for k := range filters {
4646
fields = append(fields, k)
47+
if k != "" {
48+
fields = append(fields, strings.ToUpper(string(k[:1]))+k[1:])
49+
}
4750
}
4851

4952
return fields

service/ratelimit_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ func rateLimit2Client(
521521

522522
rule := &apitraffic.Rule{}
523523
rule.Id = utils.NewStringValue(rateLimit.ID)
524+
rule.Name = utils.NewStringValue(rateLimit.Name)
524525
rule.Service = utils.NewStringValue(service)
525526
rule.Namespace = utils.NewStringValue(namespace)
526527
rule.Priority = utils.NewUInt32Value(rateLimit.Priority)

store/boltdb/ratelimit.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"reflect"
2323
"sort"
24+
"strconv"
2425
"strings"
2526
"time"
2627

@@ -170,6 +171,10 @@ func (r *rateLimitStore) GetExtendRateLimits(
170171
if !strings.Contains(m[k].(string), v) {
171172
return false
172173
}
174+
} else if k == "disable" {
175+
if v != strconv.FormatBool(m[RateLimitFieldDisable].(bool)) {
176+
return false
177+
}
173178
} else {
174179
qV := m[k]
175180
if !reflect.DeepEqual(qV, v) {

store/boltdb/ratelimit_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,62 @@ func Test_rateLimitStore_GetExtendRateLimits(t *testing.T) {
301301

302302
}
303303

304+
func Test_rateLimitStore_GetExtendRateLimitsDisable(t *testing.T) {
305+
CreateTableDBHandlerAndRun(t, "test_ratelimit_disable", func(t *testing.T, handler BoltHandler) {
306+
r := &rateLimitStore{
307+
handler: handler,
308+
}
309+
310+
svcS := &serviceStore{
311+
handler: handler,
312+
}
313+
314+
vals := make([]*model.RateLimit, 0)
315+
316+
for i := 0; i < 10; i++ {
317+
testVal := createTestRateLimit(uuid.NewString(), false)
318+
if i%2 == 0 {
319+
testVal.Disable = true
320+
} else {
321+
testVal.Disable = false
322+
}
323+
// create service
324+
svcS.AddService(&model.Service{
325+
ID: testVal.ServiceID,
326+
Name: testVal.ServiceID,
327+
Namespace: testVal.ServiceID,
328+
Owner: "Polaris",
329+
Token: testVal.Revision,
330+
})
331+
332+
vals = append(vals, testVal)
333+
if err := r.CreateRateLimit(testVal); err != nil {
334+
t.Fatalf("rateLimitStore.CreateRateLimit() error = %v", err)
335+
}
336+
}
337+
338+
// Test 1
339+
_, ret, err := r.GetExtendRateLimits(map[string]string{
340+
"disable": "true",
341+
}, 0, 100)
342+
if err != nil {
343+
t.Errorf("rateLimitStore.GetExtendRateLimits() error = %v", err)
344+
return
345+
}
346+
assert.Equal(t, int32(5), int32(len(ret)))
347+
348+
// Test 1
349+
_, ret, err = r.GetExtendRateLimits(map[string]string{
350+
"disable": "false",
351+
}, 0, 100)
352+
if err != nil {
353+
t.Errorf("rateLimitStore.GetExtendRateLimits() error = %v", err)
354+
return
355+
}
356+
assert.Equal(t, int32(5), int32(len(ret)))
357+
})
358+
}
359+
304360
func Test_rateLimitStore_GetRateLimitWithID(t *testing.T) {
305361
CreateTableDBHandlerAndRun(t, "test_ratelimit", func(t *testing.T, handler BoltHandler) {
306362
r := &rateLimitStore{

version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.14.0
1+
v1.16.0

0 commit comments

Comments
 (0)