Class: Rack::Attack::Throttle

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

Constant Summary collapse

MANDATORY_OPTIONS =
[:limit, :period].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, &block) ⇒ Throttle

Returns a new instance of Throttle.



10
11
12
13
14
15
16
17
18
19
# File 'lib/rack/attack/throttle.rb', line 10

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



8
9
10
# File 'lib/rack/attack/throttle.rb', line 8

def block
  @block
end

#limitObject (readonly)

Returns the value of attribute limit.



8
9
10
# File 'lib/rack/attack/throttle.rb', line 8

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/rack/attack/throttle.rb', line 8

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period.



8
9
10
# File 'lib/rack/attack/throttle.rb', line 8

def period
  @period
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/rack/attack/throttle.rb', line 8

def type
  @type
end

Instance Method Details

#cacheObject



21
22
23
# File 'lib/rack/attack/throttle.rb', line 21

def cache
  Rack::Attack.cache
end

#matched_by?(request) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/attack/throttle.rb', line 25

def matched_by?(request)
  discriminator = discriminator_for(request)
  return false unless discriminator

  current_period  = period_for(request)
  current_limit   = limit_for(request)
  count           = cache.count("#{name}:#{discriminator}", current_period)

  data = {
    discriminator: discriminator,
    count: count,
    period: current_period,
    limit: current_limit,
    epoch_time: cache.last_epoch_time
  }

  (count > current_limit).tap do |throttled|
    annotate_request_with_throttle_data(request, data)
    if throttled
      annotate_request_with_matched_data(request, data)
      Rack::Attack.instrument(request)
    end
  end
end