Module: Sdk4me::SendWithRetries

Included in:
Client
Defined in:
lib/sdk4me/client.rb

Instance Method Summary collapse

Instance Method Details

#_send(request, domain = @domain, port = @port, ssl = @ssl) ⇒ Object

Wraps the _send method with retries when the server does not respond, see initialize option :retries



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/sdk4me/client.rb', line 356

def _send(request, domain = @domain, port = @port, ssl = @ssl)
  return super(request, domain, port, ssl) unless option(:max_retry_time).positive?

  retries = 0
  sleep_time = 1
  now = nil
  timed_out = false
  response = nil
  loop do
    response = super(request, domain, port, ssl)
    now ||= Time.now
    if response.failure?
      sleep_time *= 2
      if (Time.now - now + sleep_time) < option(:max_retry_time)
        @logger.warn { "Request failed, retry ##{retries += 1} in #{sleep_time} seconds: #{response.message}" }
        sleep(sleep_time)
      else
        timed_out = true
      end
    end
    break unless response.failure? && !timed_out
  end
  response
end