Skip to content
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

When one of my operations has not called the atomicInt64Limiter.task() function for a long time, the speed limit may fail to be called again #106

Closed
zoutao0909 opened this issue Jun 21, 2023 · 1 comment

Comments

@zoutao0909
Copy link

Here is my test function:
func TestName(t *testing.T) {
rl := New(1)

prev := time.Now()

var sizes = 0
for i := 0; i < 100000; i++ {
	sizes++
	if sizes >= 1000 {
		now := rl.Take()
		fmt.Println(sizes, i, now.Sub(prev))
		prev = now
		if 4000 < i && i < 5000 {
			time.Sleep(30 * time.Second)
		}
		sizes = 0
	}
}

}
the result i expected:

1000 999 2µs
1000 1999 1s
1000 2999 1s
1000 3999 1s
1000 4999 1s
1000 5999 30.00147s
1000 6999 1s
1000 7999 1s
1000 8999 1s
1000 9999 1s
1000 10999 1s
1000 11999 1s
1000 12999 1s

I get the result:

1000 999 3µs
1000 1999 1s
1000 2999 1s
1000 3999 1s
1000 4999 1s
1000 5999 30.001429s
1000 6999 326µs
1000 7999 34µs
1000 8999 7µs
1000 9999 6µs
1000 10999 6µs
1000 11999 6µs
1000 12999 6µs
1000 13999 6µs
1000 14999 6µs
1000 15999 6µs
1000 16999 6µs
1000 17999 6µs
1000 18999 6µs

I got the desired result after modifying the code here:

pkg : ratelimit
fileName : limiter_atomic_int64.go
funcName : func (t *atomicInt64Limiter) Take() time.Time
line : 76

update
newTimeOfNextPermissionIssue = now - int64(t.maxSlack)
to
newTimeOfNextPermissionIssue = now)

@rabbbit
Copy link
Contributor

rabbbit commented Jul 8, 2023

This is expected - by default the limiter starts with a slack of 10.

Please use WithoutSlack option in https://github.com/uber-go/ratelimit/blob/main/ratelimit.go#L101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants