module Sdk4me::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/sdk4me/client.rb, line 320
def _send(request, domain = @domain, port = @port, ssl = @ssl)
  return super(request, domain, port, ssl) unless option(:block_at_rate_limit) && option(:max_throttle_time).positive?

  now = nil
  timed_out = false
  response = nil
  loop do
    response = super(request, domain, port, ssl)
    now ||= Time.now
    if response.throttled?
      # if no Retry-After is not provided, the 4me server is very busy, wait 5 minutes
      retry_after = response.retry_after.zero? ? 300 : [response.retry_after, 2].max
      if (Time.now - now + retry_after) < option(:max_throttle_time)
        @logger.warn { "Request throttled, trying again in #{retry_after} seconds: #{response.message}" }
        sleep(retry_after)
      else
        timed_out = true
      end
    end
    break unless response.throttled? && !timed_out
  end
  response
end