Class: WaitAMinute::WaitAMinuteRequestLog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/wait_a_minute/wait_a_minute_request_log.rb

Class Method Summary collapse

Class Method Details

.allow_request?(req = nil) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wait_a_minute/wait_a_minute_request_log.rb', line 7

def self.allow_request?(req = nil)
  return false unless req && (req.is_a?(ActionDispatch::Request) || req.is_a?(ActionController::TestRequest))
  allow = false
  ip = req.env['REMOTE_ADDR']
  if WaitAMinute.allowed_ips.include?(ip)
    Rails.logger.debug("IP #{ip} PASSING THROUGH WITHOUT REQUEST QUOTA CHECK...") if WaitAMinute.debug
    allow = true
  else
    interval = WaitAMinute.lookback_interval
    rinterval = 1.minutes
    limit = WaitAMinute.maximum_requests

    reqcount = nil
    allow = self.where(:ip => ip, :refused => true).where('created_at > ?', (Time.new - rinterval) ).count == 0
    allow &= (limit > (reqcount = self.where(:ip => ip).where('created_at > ?', (Time.new - interval) ).count)) if allow
    newreq = self.create( :ip => ip, :refused => !allow )

    Rails.logger.debug("IP #{ip} HAD #{reqcount} REQUESTS IN TIME WINDOW (max #{WaitAMinute.maximum_requests})...") if WaitAMinute.debug && !reqcount.nil?
    Rails.logger.debug("IP #{ip} HAD ALREADY REFUSED REQUESTS IN TIME WINDOW...HE WON'T STOP") if WaitAMinute.debug && reqcount.nil?
  end
  allow
end

.cleanupObject



30
31
32
33
34
35
36
37
# File 'lib/wait_a_minute/wait_a_minute_request_log.rb', line 30

def self.cleanup
  sql = ActiveRecord::Base.connection()

  sql.execute "SET autocommit=0"
  sql.begin_db_transaction
  sql.delete("DELETE FROM `#{self.name.to_s.underscore.downcase.gsub(/[a-z0-9_]+\//,'').pluralize}` WHERE created_at < '#{Time.new - WaitAMinute.lookback_interval}'")
  sql.commit_db_transaction
end