Skip to content

Commit 882281a

Browse files
authored
Add throttle limit distribution empty ratios support (#747)
* Add throttle limit distribution empty ratios support * Update CI * Fix review comments
1 parent 93ceb6a commit 882281a

File tree

3 files changed

+61
-25
lines changed

3 files changed

+61
-25
lines changed

.github/workflows/ci.yml

+12-15
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ jobs:
3434
env:
3535
GOFLAGS: ${{ matrix.flags }}
3636
LOG_LEVEL: error
37-
run: go test -coverprofile=profile.out -covermode=atomic -v -coverpkg=./... ./...
37+
run: go test -coverprofile=profile${{ matrix.flags }}.out -covermode=atomic -v -coverpkg=./... ./...
3838

3939
- name: Upload artifact
40-
uses: actions/upload-artifact@v3
40+
uses: actions/upload-artifact@v4
4141
with:
42-
name: coverage
43-
path: |
44-
profile.out
45-
profile_race.out
42+
name: coverage${{ matrix.flags }}
43+
path: profile${{ matrix.flags }}.out
4644
if-no-files-found: error
4745
retention-days: 1
4846

@@ -75,15 +73,13 @@ jobs:
7573
env:
7674
GOFLAGS: ${{ matrix.flags }}
7775
LOG_LEVEL: error
78-
run: go test ./e2e -coverprofile=profile_e2e.out -covermode=atomic -tags=e2e_new -timeout=3m -coverpkg=./...
76+
run: go test ./e2e -coverprofile=profile-e2e${{ matrix.flags }}.out -covermode=atomic -tags=e2e_new -timeout=3m -coverpkg=./...
7977

8078
- name: Upload artifact
81-
uses: actions/upload-artifact@v3
79+
uses: actions/upload-artifact@v4
8280
with:
83-
name: coverage
84-
path: |
85-
profile_e2e.out
86-
profile_e2e_race.out
81+
name: coverage-e2e${{ matrix.flags }}
82+
path: profile-e2e${{ matrix.flags }}.out
8783
if-no-files-found: error
8884
retention-days: 1
8985

@@ -97,14 +93,15 @@ jobs:
9793
uses: actions/checkout@v4
9894

9995
- name: Download artifact
100-
uses: actions/download-artifact@v3
96+
uses: actions/download-artifact@v4
10197
with:
102-
name: coverage
98+
pattern: coverage*
99+
merge-multiple: true
103100

104101
- name: Send coverage
105102
uses: codecov/codecov-action@v3
106103
with:
107-
files: profile.out, profile_race.out, profile_e2e.out, profile_e2e_race.out,
104+
files: profile.out, profile-race.out, profile-e2e.out, profile-e2e-race.out,
108105

109106
lint:
110107
runs-on: ubuntu-latest

plugin/action/throttle/distribution.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ func parseLimitDistribution(c limitDistributionCfg, totalLimit int64) (limitDist
4040
if c.Field == "" {
4141
return limitDistributions{}, nil
4242
}
43+
4344
if len(c.Ratios) == 0 {
44-
return limitDistributions{}, errors.New("empty 'ratios'")
45+
return limitDistributions{
46+
field: cfg.ParseFieldSelector(c.Field),
47+
enabled: c.Enabled,
48+
}, nil
4549
}
4650

4751
ld := limitDistributions{
@@ -103,7 +107,7 @@ type limitDistributions struct {
103107
}
104108

105109
func (ld *limitDistributions) isEnabled() bool {
106-
return ld.enabled
110+
return ld.enabled && ld.size() > 0
107111
}
108112

109113
func (ld *limitDistributions) size() int {

plugin/action/throttle/distribution_test.go

+43-8
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ func Test_parseLimitDistribution(t *testing.T) {
2121
wantErr bool
2222
}{
2323
{
24-
name: "empty_cfg",
24+
name: "empty",
2525
cfg: limitDistributionCfg{},
2626
want: limitDistributions{},
2727
},
2828
{
29-
name: "valid_cfg",
29+
name: "valid_enabled",
3030
cfg: limitDistributionCfg{
3131
Field: field,
3232
Ratios: []limitDistributionRatio{
3333
{Ratio: 0.5, Values: []string{"error"}},
3434
{Ratio: 0.35, Values: []string{"warn", "info"}},
3535
{Ratio: 0.15, Values: []string{"debug"}},
3636
},
37+
Enabled: true,
3738
},
3839
totalLimit: 100,
3940
want: limitDistributions{
@@ -48,10 +49,38 @@ func Test_parseLimitDistribution(t *testing.T) {
4849
"warn": 1, "info": 1,
4950
"debug": 2,
5051
},
52+
enabled: true,
5153
},
5254
},
5355
{
54-
name: "valid_cfg_with_def",
56+
name: "valid_disabled",
57+
cfg: limitDistributionCfg{
58+
Field: field,
59+
Ratios: []limitDistributionRatio{
60+
{Ratio: 0.5, Values: []string{"error"}},
61+
{Ratio: 0.35, Values: []string{"warn", "info"}},
62+
{Ratio: 0.15, Values: []string{"debug"}},
63+
},
64+
Enabled: false,
65+
},
66+
totalLimit: 100,
67+
want: limitDistributions{
68+
field: fieldSlice,
69+
distributions: []complexDistribution{
70+
{ratio: 0.5, limit: 50},
71+
{ratio: 0.35, limit: 35},
72+
{ratio: 0.15, limit: 15},
73+
},
74+
idxByKey: map[string]int{
75+
"error": 0,
76+
"warn": 1, "info": 1,
77+
"debug": 2,
78+
},
79+
enabled: false,
80+
},
81+
},
82+
{
83+
name: "valid_with_def",
5584
cfg: limitDistributionCfg{
5685
Field: field,
5786
Ratios: []limitDistributionRatio{
@@ -80,11 +109,16 @@ func Test_parseLimitDistribution(t *testing.T) {
80109
},
81110
},
82111
{
83-
name: "err_empty_ratios",
112+
name: "valid_empty_ratios",
84113
cfg: limitDistributionCfg{
85-
Field: field,
114+
Field: field,
115+
Enabled: true,
116+
},
117+
totalLimit: 100,
118+
want: limitDistributions{
119+
field: fieldSlice,
120+
enabled: true,
86121
},
87-
wantErr: true,
88122
},
89123
{
90124
name: "err_invalid_ratio1",
@@ -152,8 +186,9 @@ func Test_parseLimitDistribution(t *testing.T) {
152186
}
153187

154188
require.Equal(t, tt.want.field, ld.field, "wrong field")
155-
require.Equal(t, tt.want.defDistribution, ld.defDistribution, "wrong defPriority")
156-
require.Equal(t, true, slices.Equal(tt.want.distributions, ld.distributions), "wrong priorities")
189+
require.Equal(t, tt.want.defDistribution, ld.defDistribution, "wrong defDistribution")
190+
require.Equal(t, tt.want.enabled, ld.enabled, "wrong enabled")
191+
require.True(t, slices.Equal(tt.want.distributions, ld.distributions), "wrong distributions size")
157192
require.Equal(t, len(tt.want.idxByKey), len(ld.idxByKey), "wrong idxByKey size")
158193
for k, v := range ld.idxByKey {
159194
require.Equal(t, tt.want.idxByKey[k], v, fmt.Sprintf("wrong value in idxByKey with key %q", k))

0 commit comments

Comments
 (0)