Flood is a Ruby library to limit events to a count/time ratio.
Install the gem:
gem install flood
When requiring, use ‘flood’ as the gem name:
require 'flood'
There are many situations when it’s desirable to limit events that occur in a given time period. This library provides a framework for implementing rate limits.
Given a time interval and a maximum number of events that can occur in that period, the library determines whether the event should be permitted.
If you want flood control to persist across invocations of a script you must save the data. The storage method returns the flood data; you have to handle storage. To load stored flood data, call the storage= method with the flood data as a parameter.
A monitoring script should send no more than alert 1 email every 10 minutes.
email_limit = 1 email_interval = 10 * 60 # interval in seconds flood = FloodControl.new(email_limit, email_interval) if (flood.check() == 0) send_email end
A form should only accept 5 submissions from a single IP address every hour.
submit_limit = 5 submit_interval = 60 * 60 # interval in seconds flood = FloodControl.new(submit_limit, submit_interval) if (flood.check(ip_address) == 0) send_email else show_sorry_page end
Users have varying limits based on group.
TODO: Variable limits example
Save and load flood data for persistence across invocations.
TODO: Persistence example
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request.
Copyright © 2009 Neil Kohl. See LICENSE for details.