Class: Rack::Congestion::Limiter
- Inherits:
-
Object
- Object
- Rack::Congestion::Limiter
- Defined in:
- lib/rack/congestion/limiter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #backoff ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app, options = { }) ⇒ Limiter
constructor
A new instance of Limiter.
- #key ⇒ Object
- #rejected_response ⇒ Object
- #request ⇒ Object
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, = { }) self.app = app self. = end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
4 5 6 |
# File 'lib/rack/congestion/limiter.rb', line 4 def app @app end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rack/congestion/limiter.rb', line 5 def env @env end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/rack/congestion/limiter.rb', line 4 def @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 |
#backoff ⇒ Object
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 |
#key ⇒ Object
25 26 27 |
# File 'lib/rack/congestion/limiter.rb', line 25 def key ->{ 'global' } end |
#rejected_response ⇒ Object
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 |
#request ⇒ Object
21 22 23 |
# File 'lib/rack/congestion/limiter.rb', line 21 def request @request ||= Rack::Congestion::Request.new env, key, end |