-
Notifications
You must be signed in to change notification settings - Fork 336
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
[Fixes #283] Provide epoch_time that was used for the request in the data set #282
Conversation
… also be returned in the data from the throttle. This is allows access to the same time that the cache uses for the count. This can be important for clients that want to provide rate limit information for well-behaved clients
Hi @doliveirakn, thank you for implementing this PR. Could you please write a test case for the issue you reported? This ensures the Gem is actually fixing your exact problem, helps document this specific use case and catches any future regression. |
@lmansur I thought about a test case for the specific issue that was reported and I don't see a good way to write a spec for that. The reason being is that the race condition comes from the fact that the time used in the cache is not exposed. In the issue (#283) I have code that looks like this:
This PR as it stands does not fix the problem. However it does let code be written like:
The tests now assert that the |
I think you are right, the gem shouldn't really care about the user's implementation. PR looks fine for me. 👍 |
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.
Thank you for your contribution!
@@ -26,12 +26,15 @@ def [](req) | |||
current_limit = limit.respond_to?(:call) ? limit.call(req) : limit | |||
key = "#{name}:#{discriminator}" | |||
count = cache.count(key, current_period) | |||
epoch_time = cache.last_epoch_time |
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 wonder whether making the epoch time an argument to the #count
method might end up feeling a bit cleaner... Instead of keeping the last epoch time in the cache.
Not a blocker for merging though, just a consideration for a possible future refactor :-)
Aiming to solve #283
Add an reader for the epoch_time variable in the cache so that it can also be returned in the data from the throttle.
This is allows access to the same time that the cache uses for the count. This can be important for clients that want to provide rate limit information for well-behaved clients