Skip to content

Commit

Permalink
chore: set 1000 as default max limit (#4315)
Browse files Browse the repository at this point in the history
  • Loading branch information
BonapartePC committed Feb 29, 2024
1 parent 3f87930 commit 02fbd08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion router/throttler/adaptive.go
Expand Up @@ -16,6 +16,8 @@ type adaptiveThrottleConfig struct {
maxLimit func() int64
}

const adaptiveDefaultMaxLimit = 1000 // 1000 requests per second

func (c *adaptiveThrottleConfig) readThrottlingConfig(config *config.Config, destName, destID string) {
c.window = config.GetReloadableDurationVar(1, time.Second,
fmt.Sprintf(`Router.throttler.%s.%s.timeWindow`, destName, destID),
Expand All @@ -36,12 +38,17 @@ func (c *adaptiveThrottleConfig) readThrottlingConfig(config *config.Config, des
limit := config.GetReloadableInt64Var(0, 1,
fmt.Sprintf(`Router.throttler.%s.%s.limit`, destName, destID),
fmt.Sprintf(`Router.throttler.%s.limit`, destName))
defaultMaxLimit := config.GetReloadableInt64Var(adaptiveDefaultMaxLimit, 1, `Router.throttler.adaptive.defaultMaxLimit`)
c.maxLimit = func() int64 {
maxLimit := maxLimit.Load()
staticLimit := limit.Load()
staticLimitMultiplier := limitMultiplier.Load()
if maxLimit > 0 {
return maxLimit
} else if staticLimit > 0 && staticLimitMultiplier > 0 {
return int64(float64(staticLimit) * staticLimitMultiplier)
}
return int64(float64(limit.Load()) * limitMultiplier.Load())
return defaultMaxLimit.Load()
}
}

Expand Down
10 changes: 10 additions & 0 deletions router/throttler/factory_test.go
Expand Up @@ -103,6 +103,16 @@ func TestFactory(t *testing.T) {
"destType": "destName",
}).LastValue())
})

t.Run("when no limit is set", func(t *testing.T) {
conf := config.New()
conf.Set("Router.throttler.adaptive.enabled", true)
f, err := NewFactory(conf, nil)
require.NoError(t, err)
defer f.Shutdown()
ta := f.Get("destName", "destID")
require.EqualValues(t, adaptiveDefaultMaxLimit, ta.getLimit())
})
}

func floatCheck(a, b int64) bool {
Expand Down

0 comments on commit 02fbd08

Please sign in to comment.