Class: Rack::Attack::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/attack/throttle.rb

Constant Summary collapse

MANDATORY_OPTIONS =
[:limit, :period]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, block) ⇒ Throttle

Returns a new instance of Throttle.



6
7
8
9
10
11
12
13
# File 'lib/rack/attack/throttle.rb', line 6

def initialize(name, options, block)
  @name, @block = name, block
  MANDATORY_OPTIONS.each do |opt|
    raise ArgumentError.new("Must pass #{opt.inspect} option") unless options[opt]
  end
  @limit  = options[:limit]
  @period = options[:period].to_i
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/rack/attack/throttle.rb', line 5

def block
  @block
end

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/rack/attack/throttle.rb', line 5

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rack/attack/throttle.rb', line 5

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period.



5
6
7
# File 'lib/rack/attack/throttle.rb', line 5

def period
  @period
end

Instance Method Details

#[](req) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/attack/throttle.rb', line 19

def [](req)
  discriminator = block[req]
  return false unless discriminator

  key = "#{name}:#{discriminator}"
  count = cache.count(key, period)
  current_limit = limit.respond_to?(:call) ? limit.call(req) : limit
  data = {
    :count => count,
    :period => period,
    :limit => current_limit
  }
  (req.env['rack.attack.throttle_data'] ||= {})[name] = data

  (count > current_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

#cacheObject



15
16
17
# File 'lib/rack/attack/throttle.rb', line 15

def cache
  Rack::Attack.cache
end