Passing a proc as period value results in count being reset #559
Replies: 2 comments
-
Hi @djpowers , I think this is a misunderstanding of what Rack::Attack's The logic's all in What's happening in your case is that the current unix epoch time is about 1.6 billion. Let's say it's 1.6 billion exactly. When you invoke your "daily" throttle, If your lambda is invoked even 1 second later, you define a period of 43099 seconds, so every piece from the beginning of the unix epoch to now is slightly smaller, the whole thing ratchets up, and 1.6 billion / 43099 puts us in piece 37123. By the time we get near You didn't notice this when using Unfortunately there's no way to do this currently in Rack::Attack, because months are of varying length and whatever period value you pick you're not going to hit the beginning and end of any month precisely. If you want that behavior, you're going to have to fork the gem and add that functionality to |
Beta Was this translation helpful? Give feedback.
-
Hi @jamiemccarthy, thanks for the breakdown. It seems like the idea in #81 to add an |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing some odd behavior while attempting to pass a proc as an argument to a throttle
period
.I tried following the suggestion to use a proc by @ktheory in #81 (comment), as my use case is very similar.
The following code appears to work fine for me:
However, when I changed the
period_proc
above to use#end_of_day
instead, the value is calculated properly, but each request seems to result in a new throttle being created, and thecount
value never surpasses 1:My understanding is that the ID showing up in the logs corresponds to a current throttle period, and a new one is created after a current throttle period expires. This ID was being incremented on each new request, even though the period had not ended
The ID using the
#end_of_month
code remained consistent, as far as I saw.Out of curiosity, I tried using
#end_of_week
, and the count would reset seemingly sporadically, creating showing an incremented ID when the count went back to 1 (without hitting the limit).It seems that the problem is actually occurring in each scenario, but when the proc yields lower values, it happens more reliably. My guess is that something is busting the cache, but I'm not sure why, or how to explain the discrepancy in behavior.
I'm using v5.4.2 of the gem on Ruby 2.2 with
ActiveSupport::Cache::DalliStore
.Any guidance is very much appreciated!
Beta Was this translation helpful? Give feedback.
All reactions