Class: Rack::Attack::Throttle
- Inherits:
-
Object
- Object
- Rack::Attack::Throttle
- Defined in:
- lib/rack/attack/throttle.rb
Constant Summary collapse
- MANDATORY_OPTIONS =
[:limit, :period].freeze
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cache ⇒ Object
-
#initialize(name, options, block) ⇒ Throttle
constructor
A new instance of Throttle.
- #matched_by?(request) ⇒ Boolean
Constructor Details
#initialize(name, options, block) ⇒ Throttle
Returns a new instance of Throttle.
7 8 9 10 11 12 13 14 15 |
# File 'lib/rack/attack/throttle.rb', line 7 def initialize(name, , block) @name, @block = name, block MANDATORY_OPTIONS.each do |opt| raise ArgumentError.new("Must pass #{opt.inspect} option") unless [opt] end @limit = [:limit] @period = [:period].respond_to?(:call) ? [:period] : [:period].to_i @type = .fetch(:type, :throttle) end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
6 7 8 |
# File 'lib/rack/attack/throttle.rb', line 6 def block @block end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
6 7 8 |
# File 'lib/rack/attack/throttle.rb', line 6 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/rack/attack/throttle.rb', line 6 def name @name end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
6 7 8 |
# File 'lib/rack/attack/throttle.rb', line 6 def period @period end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/rack/attack/throttle.rb', line 6 def type @type end |
Instance Method Details
#cache ⇒ Object
17 18 19 |
# File 'lib/rack/attack/throttle.rb', line 17 def cache Rack::Attack.cache end |
#matched_by?(request) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rack/attack/throttle.rb', line 21 def matched_by?(request) discriminator = block.call(request) return false unless discriminator current_period = period.respond_to?(:call) ? period.call(request) : period current_limit = limit.respond_to?(:call) ? limit.call(request) : limit key = "#{name}:#{discriminator}" count = cache.count(key, current_period) epoch_time = cache.last_epoch_time data = { :count => count, :period => current_period, :limit => current_limit, :epoch_time => epoch_time } (request.env['rack.attack.throttle_data'] ||= {})[name] = data (count > current_limit).tap do |throttled| if throttled request.env['rack.attack.matched'] = name request.env['rack.attack.match_discriminator'] = discriminator request.env['rack.attack.match_type'] = type request.env['rack.attack.match_data'] = data Rack::Attack.instrument(request) end end end |