module Itrp::SendWithRateLimitBlock

Public Instance Methods

_send(request, domain = @domain, port = @port, ssl = @ssl) click to toggle source

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

Calls superclass method
# File lib/itrp/client.rb, line 302
def _send(request, domain = @domain, port = @port, ssl = @ssl)
  return super(request, domain, port, ssl) unless option(:block_at_rate_limit)
  now = Time.now
  timed_out = false
  # respect the max_retry_time with fallback to max 1 hour and 1 minute wait time
  max_retry_time = option(:max_retry_time) > 0 ? option(:max_retry_time) : 3660
  begin
    _response = super(request, domain, port, ssl)
    if _response.throttled?
      retry_after = _response.retry_after == 0 ? 300 : [_response.retry_after, 2].max
      if (Time.now - now + retry_after) < max_retry_time
        @logger.warn { "Request throttled, trying again in #{retry_after} seconds: #{_response.message}" }
        sleep(retry_after)
      else
        timed_out = true
      end
    end
  end while _response.throttled? && !timed_out
  _response
end