Class: Throttling::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/throttling/base.rb

Overview

Class implements throttling for a single action.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/throttling/base.rb', line 6

def initialize(action)
  @action = action.to_s

  raise ArgumentError, "No throttling limits specified" unless Throttling.limits
  @limits = Throttling.limits[action]
  raise ArgumentError, "No Throttling.limits[#{action}] section found" unless limits

  # Convert simple limits to a hash
  if @limits[:period]
    if @limits[:values]
      @limits[:values] = @limits[:values].sort_by { |name, params| params && params[:limit] }
    end
    @limits = [[ 'global', @limits ]]
  else
    @limits = @limits.sort_by { |name, params| params && params[:period] }
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/throttling/base.rb', line 4

def action
  @action
end

#limitsObject

Returns the value of attribute limits.



4
5
6
# File 'lib/throttling/base.rb', line 4

def limits
  @limits
end

Instance Method Details

#check(check_type, check_value, auto_increment = true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/throttling/base.rb', line 32

def check(check_type, check_value, auto_increment = true)
  # Disabled?
  return true if !Throttling.enabled? || check_value.nil?

  limits.each do |period_name, params|
    period = params[:period].to_i
    limit  = params[:limit].nil? ? nil : params[:limit].to_i
    values = params[:values]

    raise ArgumentError, "Invalid or no 'period' parameter in the limits[#{action}][#{period_name}] config: #{limit.inspect}" if period < 1
    raise ArgumentError, "Invalid 'limit' parameter in the limits[#{action}][#{period_name}] config: #{limit.inspect}" if !limit.nil? && limit < 0

    key = hits_store_key(check_type, check_value, period_name, period)

    # Retrieve current value
    hits = Throttling.storage.fetch(key, :expires_in => hits_store_ttl(period), :raw => true) { '0' }.to_i

    if values
      value = params[:default_value] || false
      values.each do |value_name, value_params|
        if hits < value_params[:limit].to_i
          value = value_params[:value] || value_params[:default_value] || false
          break
        end
      end
    else
      # Over limit?
      return false if !limit.nil? && hits >= limit
    end

    Throttling.storage.increment(key) if auto_increment
    return value if values
  end

  return true
end

#check_ip(ip) ⇒ Object



24
25
26
# File 'lib/throttling/base.rb', line 24

def check_ip(ip)
  check(:ip, ip)
end

#check_user_id(user_id) ⇒ Object



28
29
30
# File 'lib/throttling/base.rb', line 28

def check_user_id(user_id)
  check(:user_id, user_id)
end