-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow configuring custom slack #64
Conversation
Codecov Report
@@ Coverage Diff @@
## main #64 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 3 3
Lines 69 70 +1
=========================================
+ Hits 69 70 +1
Continue to review full report at Codecov.
|
// - faster limiter running for 1 second | ||
// - slack accumulated by the faster limiter during the two seconds. | ||
// it was blocked by slower limiter. | ||
tests := []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also had the same test generated dynamically:
const (
slowRate = 10
fastRate = 100
)
// Test for a few Per settings.
testsPer := []time.Duration{
// Default.
{time.Second},
{time.Millisecond * 500},
}
// And a few different slack options
testsSlack := []int{10, 20, 100, 150}
for _, per := range testsPer {
for _, slack := range testsSlack {
testKey := fmt.String("per-%v-slack-%v", per, slack)
t.Run(testKey, func(t *testing.T) {
runTest(t, func(r testRunner) {
slow := r.createLimiter(slowRate, WithoutSlack)
fast := r.createLimiter(fastRate, tt.opt...)
r.startTaking(slow, fast)
r.afterFunc(2, func() {
r.startTaking(fast)
r.startTaking(fast)
})
// slow limiter with 10hz dominates here - we're always at it's limit.
r.assertCountAt(1*time.Second, slowRate)
// results:
// - for two seconds we ran a slow rate
// - for a second we ran a fast rate
// - and we add the slack
// rates have to be adjusted for Per
adjustedSlow := slowRate * int(time.Second/testsPer)
asjustedFast := fastRate * int(time.Second/testsPer)
r.assertCountAt(3*time.Second, asjustedSlow+adjustedFast+slack)
})
})
}
}
but it seems like manual definition is easier to understand at this point.
This is a split off #64 to make #64 smaller/easier. I believe out current treatment of the default (10) slack doesn't work very well with Per option. We configure the limiter with an limit: - `Limiter(10)` - means, allow 10 requests per second, with 10 requests slack - "slack period" is 1 second However, if we do: - `Limiter(10, Per(time.Minute))` we get - 10 requests per minute, but the slack is still 10 *per second*, meaning the slack period is stil 10 second - effectively 1 request. I believe this is confusing, because with "slack of 10", I'd expect a 1 minute "slack period". After this change: - `Limiter(10, Per(time.Minute))` we get - 10 requests per minute, and a slack of 10 requests - so, slack period is 1 minute. We're basically making it so that both "rate" and "slack" are expressed in the same unit (ints) and that "Per" applies to both of them. Note this is currently of minor impact since we're hardcoding the slack as 10. This will become way more painful/complicated with #64.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This is a split off #64 to make #64 smaller/easier. I believe out current treatment of the default (10) slack doesn't work very well with Per option. We configure the limiter with an limit: - `Limiter(10)` - means, allow 10 requests per second, with 10 requests slack - "slack period" is 1 second However, if we do: - `Limiter(10, Per(time.Minute))` we get - 10 requests per minute, but the slack is still 10 *per second*, meaning the slack period is stil 10 second - effectively 1 request. I believe this is confusing, because with "slack of 10", I'd expect a 1 minute "slack period". After this change: - `Limiter(10, Per(time.Minute))` we get - 10 requests per minute, and a slack of 10 requests - so, slack period is 1 minute. We're basically making it so that both "rate" and "slack" are expressed in the same unit (ints) and that "Per" applies to both of them. Note this is currently of minor impact since we're hardcoding the slack as 10. This will become way more painful/complicated with #64.
72c0901
to
7ae41d6
Compare
Resolves #20 This is to allow people to configure custom slack - we previously defaulted to 10, and only allowed to disable it. The tests are a bit painful to read, I had a stab at generating the same set of tests for both with, and without per, but that was even more hard to read.
Resolves #20
This is to allow people to configure custom slack - we previously
defaulted to 10, and only allowed to disable it.
The only tricky part about this is handling "per" correctly - adding
a bunch of test cases to cover it.
The tests are large, but when I tried to generate them dynamically
(the same test cases 2*, for both with-per and without-per) it became
even harder to read.