vinova/voteable_mongo — Issue #23: Rate limiting?
Author: barmstrong
Created: 2011-11-16 04:31:52+00:00
State: open

Has anyone gotten rate limiting to work?

It doesn't look like there is a time stamp on any of the votes by default so a classic solution of "how many votes did this user create in the last minute" doesn't seem like it would work.

I'll share one alternate solution that I just got working. It keeps an timestamp on each user which starts out being 10 minutes ago (and can never be less than this). It then increments the timestamp by 30 seconds for each action the user takes, and blocks additional actions if the timestamp is > Time.now

class User
...
field :rate_limit, :type => Integer

...
# checks AND updates rate limit
def over_rate_limit?
window = 10.minutes.ago.to_i
interval = 30.seconds.to_i

return true if rate_limit.present? and rate_limit > Time.now.to_i
if rate_limit.nil? or rate_limit < window
self.rate_limit = window
end
self.rate_limit += interval
self.save!
false
end
end

Any other solutions?