Skip to content

Commit

Permalink
Remove the need to specify unit for unlimited descriptors
Browse files Browse the repository at this point in the history
Signed-off-by: lmajercak-wish <[email protected]>
  • Loading branch information
lmajercak-wish committed Jun 17, 2021
1 parent df4f71a commit 6a81d9b
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ domain: internal
descriptors:
- key: ldap
rate_limit:
unit: second
unlimited: true

- key: azure
Expand Down
2 changes: 0 additions & 2 deletions examples/ratelimit/config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ descriptors:
requests_per_unit: 1
- key: bay
rate_limit:
unit: second
unlimited: true
- key: qux
rate_limit:
unit: minute
unlimited: true
6 changes: 4 additions & 2 deletions src/config/config_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,18 @@ func (this *rateLimitDescriptor) loadDescriptors(config RateLimitConfigToLoad, p
var rateLimit *RateLimit = nil
var rateLimitDebugString string = ""
if descriptorConfig.RateLimit != nil {
unlimited := descriptorConfig.RateLimit.Unlimited

value, present :=
pb.RateLimitResponse_RateLimit_Unit_value[strings.ToUpper(descriptorConfig.RateLimit.Unit)]
if !present || value == int32(pb.RateLimitResponse_RateLimit_UNKNOWN) {
if (!present || value == int32(pb.RateLimitResponse_RateLimit_UNKNOWN)) && !unlimited {
panic(newRateLimitConfigError(
config,
fmt.Sprintf("invalid rate limit unit '%s'", descriptorConfig.RateLimit.Unit)))
}

rateLimit = NewRateLimit(
descriptorConfig.RateLimit.RequestsPerUnit, pb.RateLimitResponse_RateLimit_Unit(value), statsManager.NewStats(newParentKey), descriptorConfig.RateLimit.Unlimited)
descriptorConfig.RateLimit.RequestsPerUnit, pb.RateLimitResponse_RateLimit_Unit(value), statsManager.NewStats(newParentKey), unlimited)
rateLimitDebugString = fmt.Sprintf(
" ratelimit={requests_per_unit=%d, unit=%s, unlimited=%t}", rateLimit.Limit.RequestsPerUnit,
rateLimit.Limit.Unit.String(), rateLimit.Unlimited)
Expand Down
1 change: 0 additions & 1 deletion test/config/basic_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ descriptors:

- key: key6
rate_limit:
unit: day
unlimited: true
1 change: 0 additions & 1 deletion test/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func TestBasicConfig(t *testing.T) {
})
rl.Stats.TotalHits.Inc()
rl.Stats.WithinLimit.Inc()
assert.Equal(pb.RateLimitResponse_RateLimit_DAY, rl.Limit.Unit)
assert.True(rl.Unlimited)
assert.EqualValues(1, stats.NewCounter("test-domain.key6.total_hits").Value())
assert.EqualValues(1, stats.NewCounter("test-domain.key6.within_limit").Value())
Expand Down

0 comments on commit 6a81d9b

Please sign in to comment.