Class: Rack::Attack::Throttle
- Inherits:
-
Object
- Object
- Rack::Attack::Throttle
- Defined in:
- lib/rack/attack/throttle.rb
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.
Instance Method Summary collapse
- #[](req) ⇒ Object
- #cache ⇒ Object
-
#initialize(name, options, block) ⇒ Throttle
constructor
A new instance of Throttle.
Constructor Details
#initialize(name, options, block) ⇒ Throttle
Returns a new instance of Throttle.
5 6 7 8 9 10 11 12 |
# File 'lib/rack/attack/throttle.rb', line 5 def initialize(name, , block) @name, @block = name, block [:limit, :period].each do |opt| raise ArgumentError.new("Must pass #{opt.inspect} option") unless [opt] end @limit = [:limit] @period = [:period] end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
4 5 6 |
# File 'lib/rack/attack/throttle.rb', line 4 def block @block end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
4 5 6 |
# File 'lib/rack/attack/throttle.rb', line 4 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/rack/attack/throttle.rb', line 4 def name @name end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
4 5 6 |
# File 'lib/rack/attack/throttle.rb', line 4 def period @period end |
Instance Method Details
#[](req) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rack/attack/throttle.rb', line 18 def [](req) discriminator = block[req] return false unless discriminator key = "#{name}:#{discriminator}" count = cache.count(key, period) data = { :count => count, :period => period, :limit => limit } (req.env['rack.attack.throttle_data'] ||= {})[name] = data (count > limit).tap do |throttled| if throttled req.env['rack.attack.matched'] = name req.env['rack.attack.match_type'] = :throttle req.env['rack.attack.match_data'] = data Rack::Attack.instrument(req) end end end |