Class: Etna::Client::Retrier

Inherits:
Object
  • Object
show all
Defined in:
lib/etna/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(ignore_ssl: false, max_retries: 10, backoff_time: 15, logger:) ⇒ Retrier

Ideally the retry code would be centralized with metis_client …

unsure what would be the best approach to do that, at this moment.


238
239
240
241
242
243
# File 'lib/etna/client.rb', line 238

def initialize(ignore_ssl: false, max_retries: 10, backoff_time: 15, logger:)
  @max_retries = max_retries
  @ignore_ssl = ignore_ssl
  @backoff_time = backoff_time
  @logger = logger
end

Instance Method Details

#retry_request(uri, req, retries: 0, &block) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/etna/client.rb', line 245

def retry_request(uri, req, retries: 0, &block)
  retries += 1

  begin
    @logger.debug("\rWaiting for #{uri.host} restart"+"."*retries+"\x1b[0K")

    sleep @backoff_time * retries
  end if retries > 1

  if retries < @max_retries
    begin
      if block_given?
        request(uri, req) do |block_response|
          if net_exceptions.include?(block_response.class)
            @logger.debug("Received #{block_response.class.name}, retrying")
            retry_request(uri, req, retries: retries, &block)
          elsif block_response.is_a?(OpenSSL::SSL::SSLError)
            @logger.debug("SSL error, retrying")
            retry_request(uri, req, retries: retries, &block)
          else
            status_check!(block_response)
            yield block_response
          end
        end
      else
        response = request(uri, req)
      end
    rescue OpenSSL::SSL::SSLError => e
      if e.message =~ /write client hello/
        @logger.debug("SSL error, retrying")
        return retry_request(uri, req, retries: retries)
      end
      raise e
    rescue *net_exceptions => e
      @logger.debug("Received #{e.class.name}, retrying")
      return retry_request(uri, req, retries: retries)
    end

    begin
      retry_codes = ['503', '502', '504', '408']
      if retry_codes.include?(response.code)
        @logger.debug("Received response with code #{response.code}, retrying")
        return retry_request(uri, req, retries: retries)
      elsif response.code == '500' && response.body.start_with?("Puma caught")
        @logger.debug("Received 500 Puma error #{response.body.split("\n").first}, retrying")
        return retry_request(uri, req, retries: retries)
      end

      status_check!(response)
      return response
    end unless block_given?
  else
    raise ::Etna::Error, "Could not contact server, giving up"
  end
end