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

Implement concurrency limit template #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

masim05
Copy link
Contributor

@masim05 masim05 commented Jan 5, 2021

Sometimes it is required to limit concurrency without RPS limitation.
Let's consider a situation we have a function which takes 100 ms and we want to have 3 or less calls of the function at any moment. We may use ratelimit template with parameters {concurrentRequests: 3, rps: 30}.
Let's consider a situation we have a function which takes 1 s and we want to have 3 or less calls of the function at any moment. We may use ratelimit template with parameters {concurrentRequests: 3, rps: 3}.
The problem here is that rps parameter depends on the time of the function's execution which is usually unknown and is not a constant.
In other words if we don't want to limit RPS an interface instrumented with ratelimit template doesn't limit concurrency. Take a look at https://github.com/masim05/gowrap/blob/concurrency-limit-motivation/templates_tests/interface_with_ratelimit_test.go#L37 (which fails) for more details.

Since the change will be backward incompatible with current implementation of ratelimit template (in terms of runtime behaviour) it is worth having a separate template for concurrency limitation. This PR provides the implementation.

@masim05 masim05 marked this pull request as ready for review January 5, 2021 09:29
@coveralls
Copy link

Pull Request Test Coverage Report for Build 107

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 96.639%

Totals Coverage Status
Change from base Build 106: 0.0%
Covered Lines: 230
Relevant Lines: 238

💛 - Coveralls

@hexdigest
Copy link
Owner

@masim05 my only practical question is that if you want to limit concurrency without limiting RPS why not just use a high enough number for RPS?

The other thing I was thinking about is that modifying the existing template in a way that zero RPS means no RPS limiting would be better than having multiple templates doing almost the same thing.

@masim05
Copy link
Contributor Author

masim05 commented Jan 6, 2021

my only practical question is that if you want to limit concurrency without limiting RPS why not just use a high enough number for RPS?

This is exactly the point: if you use big number as rps parameter and every call lasts long enough, you may end up in a situation when many calls are running simultaneously as shown here. In other words, concurrency is not limited.

The other thing I was thinking about is that modifying the existing template in a way that zero RPS means no RPS limiting would be better than having multiple templates doing almost the same thing.

Sounds reasonable.

@masim05
Copy link
Contributor Author

masim05 commented Jan 7, 2021

@hexdigest
It took me some time to realize that rate limiting and concurrency limiting are not the same.
Rate limit does not limit concurrency if call executions last long enough. Concurrency limit does not guarantee rate limit if call executions are fast enough.
Don't you think it would be nice to have two independent templates and thus follow unix philosophy? =)

@@ -63,6 +63,7 @@ If the file is not found, gowrap will look for the template [here](https://githu

List of available templates:
- [circuitbreaker](https://github.com/hexdigest/gowrap/tree/master/templates/circuitbreaker) stops executing methods of the wrapped interface after the specified number of consecutive errors and resumes execution after the specified delay
- [concurrencylimit](https://github.com/hexdigest/gowrap/tree/master/templates/concurrencylimit) limits amount of simultaneous calls
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"limits THE amount" please

@hexdigest
Copy link
Owner

@masim05 when I got back to you PR, ran tests and look at the original ratelimit template I realized that there was no intention to limit concurrency with this decorator. The problem is that concurrentRequests parameter has a misleading name. I renamed it to "burst". This is the amount of requests that the source interface can handle during the second/rps interval.

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

Successfully merging this pull request may close these issues.

3 participants