Class: Rack::Congestion::Limiter

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/congestion/limiter.rb

Direct Known Subclasses

IpLimiter, PathLimiter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = { }) ⇒ Limiter

Returns a new instance of Limiter.



7
8
9
10
# File 'lib/rack/congestion/limiter.rb', line 7

def initialize(app, options = { })
  self.app = app
  self.options = options
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



4
5
6
# File 'lib/rack/congestion/limiter.rb', line 4

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/rack/congestion/limiter.rb', line 5

def env
  @env
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/rack/congestion/limiter.rb', line 4

def options
  @options
end

Instance Method Details

#_call(env) ⇒ Object



16
17
18
19
# File 'lib/rack/congestion/limiter.rb', line 16

def _call(env)
  @env = env
  request.allowed? ? app.call(env) : rejected_response
end

#backoffObject



40
41
42
# File 'lib/rack/congestion/limiter.rb', line 40

def backoff
  request.backoff.to_s rescue -1
end

#call(env) ⇒ Object



12
13
14
# File 'lib/rack/congestion/limiter.rb', line 12

def call(env)
  dup._call env
end

#keyObject



25
26
27
# File 'lib/rack/congestion/limiter.rb', line 25

def key
  ->{ 'global' }
end

#rejected_responseObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/congestion/limiter.rb', line 29

def rejected_response
  [
    429,
    {
      'Content-Type' => 'text/plain; charset=utf-8',
      'Retry-After' => backoff
    },
    ['Rate Limit Exceeded']
  ]
end

#requestObject



21
22
23
# File 'lib/rack/congestion/limiter.rb', line 21

def request
  @request ||= Rack::Congestion::Request.new env, key, options
end