Class: Zendesk2::RateLimit
- Inherits:
-
Object
- Object
- Zendesk2::RateLimit
- Defined in:
- lib/zendesk2/rate_limit.rb
Overview
Faraday middleware to block and sleep on rate limit events
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ RateLimit
constructor
A new instance of RateLimit.
Constructor Details
#initialize(app, options = {}) ⇒ RateLimit
Returns a new instance of RateLimit.
8 9 10 11 |
# File 'lib/zendesk2/rate_limit.rb', line 8 def initialize(app, = {}) @app = app @logger = [:logger] || ::Logger.new(nil) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/zendesk2/rate_limit.rb', line 6 def logger @logger end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/zendesk2/rate_limit.rb', line 13 def call(env) original_env = env.dup response = @app.call(env) if response.env[:status] == 429 delay = (response.env[:response_headers][:retry_after]).to_f logger.warn "Rate limited! Retrying after #{delay} seconds." sleep(delay) call(original_env).tap { |r| increment(r, delay) } else response end end |