Class: SlowWeb::Limit

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_web/limit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, count, period) ⇒ Limit

Returns a new instance of Limit.

Parameters:

  • host (String)

    the host to restrict.

  • count (Fixnum)

    the number of requests that can occur within a time period.

  • period (Fixnum)

    the number of seconds in the time period.



13
14
15
16
17
18
19
# File 'lib/slow_web/limit.rb', line 13

def initialize(host, count, period)
  @host   = host
  @count  = count
  @period = period

  @requests = []
end

Instance Attribute Details

#countObject

The number of requests that are allowed within the time period.



30
31
32
# File 'lib/slow_web/limit.rb', line 30

def count
  @count
end

#hostObject

The host to restrict.



27
28
29
# File 'lib/slow_web/limit.rb', line 27

def host
  @host
end

#periodObject

The number of seconds in the time period.



33
34
35
# File 'lib/slow_web/limit.rb', line 33

def period
  @period
end

Instance Method Details

#add_request(request) ⇒ Object

Adds a request that is associated with this limit

Parameters:

  • request (Net::HTTPRequest)

    the request associated with this limit.



54
55
56
57
58
# File 'lib/slow_web/limit.rb', line 54

def add_request(request)
  @requests << {:obj => request, :time => Time.now}
  normalize_requests()
  nil
end

#current_request_countObject

The number of requests that have occurred within the current period.



36
37
38
39
# File 'lib/slow_web/limit.rb', line 36

def current_request_count
  normalize_requests()
  return @requests.length
end

#exceeded?Boolean

A flag stating if the number of requests within the period has been met.

Returns:

  • (Boolean)


42
43
44
# File 'lib/slow_web/limit.rb', line 42

def exceeded?
  return current_request_count >= count
end