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

[Question] Why sleepFor should >= maxSlack in limiter_atomic.go #63

Closed
jiekun opened this issue Jan 24, 2021 · 3 comments
Closed

[Question] Why sleepFor should >= maxSlack in limiter_atomic.go #63

jiekun opened this issue Jan 24, 2021 · 3 comments

Comments

@jiekun
Copy link

jiekun commented Jan 24, 2021

Hi maintainers.

I am new to Go. And would like to raise this issue for the question about:
In Take() method in limiter_atomic.go, sleepFor attr represents how much time we should sleep:

newState.sleepFor += t.perRequest - now.Sub(oldState.last)

So for example we set atelimit.New(1), and last Take() triggered 20 seconds ago. Now newState.sleepFor is calculated as -19.

This attr is used later in t.clock.Sleep(newState.sleepFor). Other than that, it's useless ( Not sure if I am correct). So why sleepFor should >= maxSlack ? I think Sleep(-19) is the same as Sleep(-10) ?

Thank you for your reply in advance :)

@rabbbit
Copy link
Contributor

rabbbit commented Jan 25, 2021

The key is that sleepFor can be used not in just this iteration, but in the following ones - we keep it for future iterations, to keep track how many "slack" requests we have available.

Technically, you're right - if sleepFor is negative, we indeed don't sleep at all - so from that perspective it's the same to have Sleep(-19) and Sleep(-10).

This could have been just written as

if newState.sleepFor > 0 {
    t.clock.Sleep(newState.sleepFor)
}

and would be equivalent.

Hope this helps - if not, please ask again :)

@rabbbit rabbbit closed this as completed Jan 25, 2021
@jiekun
Copy link
Author

jiekun commented Jan 25, 2021

Yes I considered this could be (but currently not) used in other cases.

Thank you very much. clear enough

@rabbbit
Copy link
Contributor

rabbbit commented Jan 25, 2021 via email

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