Class: ZendeskAPI::Middleware::Request::Retry
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ZendeskAPI::Middleware::Request::Retry
- Defined in:
- lib/zendesk_api/middleware/request/retry.rb
Overview
Faraday middleware to handle HTTP Status 429 (rate limiting) / 503 (maintenance)
Constant Summary collapse
- DEFAULT_RETRY_AFTER =
10
- ERROR_CODES =
[429, 503]
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Retry
constructor
A new instance of Retry.
Constructor Details
#initialize(app, options = {}) ⇒ Retry
Returns a new instance of Retry.
13 14 15 16 |
# File 'lib/zendesk_api/middleware/request/retry.rb', line 13 def initialize(app, ={}) super(app) @logger = [:logger] end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/zendesk_api/middleware/request/retry.rb', line 18 def call(env) original_env = env.dup response = @app.call(env) if ERROR_CODES.include?(response.env[:status]) seconds_left = (response.env[:response_headers][:retry_after] || DEFAULT_RETRY_AFTER).to_i @logger.warn "You have been rate limited. Retrying in #{seconds_left} seconds..." if @logger seconds_left.times do |i| sleep 1 time_left = seconds_left - i @logger.warn "#{time_left}..." if time_left > 0 && time_left % 5 == 0 && @logger end @logger.warn "" if @logger @app.call(original_env) else response end end |