Browse files

add some comments

  • Loading branch information...
1 parent 7f8d4a3 commit 061d7de57cc46ba8de194374ceabf995c3542514 @Geal committed Jul 12, 2012
Showing with 18 additions and 0 deletions.
  1. +18 −0 lib/rate_limit.rb
View
18 lib/rate_limit.rb
@@ -3,16 +3,27 @@
require "rate_limit/visit"
module RateLimit
+
+ ##
+ # Block requests by IP
+ # the request argument is given by the request method of Rails controllers
+
def self.limitByIp(request, requests_per_seconds = 1, cache = Rails.cache)
key = request.remote_ip+request.fullpath
blockByKey(key, requests_per_seconds, cache)
end
+ ##
+ # Slow requests by IP
+ # the factor parameter is the multiplicating factor for the growing time between requests
def self.slowByIp(request, requests_per_seconds = 1, factor = 1, cache = Rails.cache)
key = request.remote_ip+request.fullpath
slowByKey(key, requests_per_seconds, factor, cache)
end
+ ##
+ # Block requests by key
+ # You can define wich eky is used to discriminate between requests
def self.blockByKey(key, requests_per_seconds = 1, cache = Rails.cache)
loader = Loader.new cache
@@ -30,6 +41,9 @@ def self.blockByKey(key, requests_per_seconds = 1, cache = Rails.cache)
end
end
+ ##
+ # Slow requests by key
+ # You can define wich eky is used to discriminate between requests
def self.slowByKey(key, requests_per_seconds = 0.5, factor = 1, cache = Rails.cache)
loader = Loader.new cache
@@ -54,11 +68,15 @@ def self.slowByKey(key, requests_per_seconds = 0.5, factor = 1, cache = Rails.ca
end
end
+ ##
+ # Reset the counters for blocked requests
def self.resetBlockerByKey(key)
loader = Loader.new cache
cache.delete(key+"b")
end
+ ##
+ # Reset the counters for slowed requests
def self.resetSlowerByKey(key)
loader = Loader.new cache
cache.delete(key+"s")

0 comments on commit 061d7de

Please sign in to comment.