Sha256: f4e4151b2e97ae632e1cc9e1b5b5d77ec8126dd94f2e2a1e288f04a619cf9cf1

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

ShopifyAPI::Connection.class_eval do
  alias_method :shopify_request, :request

  def request(*args)
    network_retry = 0
    network_limit = 10

    rate_limit_retry = 0
    rate_limit_limit = 30

    begin
      shopify_request(*args)
    rescue Exception => e
      exceptions_to_retry = [
        ActiveResource::ClientError,
        Errno::ECONNRESET,
        Errno::ETIMEDOUT,
        Errno::EHOSTUNREACH,
        EOFError,
        Zlib::BufError,
        SocketError,
        ActiveResource::SSLError,
        ActiveResource::TimeoutError,
        # NOTE represents only 500x errors
        ActiveResource::ServerError
      ]

      if !exceptions_to_retry.include?(e.class)
        raise
      end

      if e.class == ActiveResource::ClientError && e.message =~ /429/ && e.message =~ /Too Many Requests/
        if rate_limit_retry > 0 && rate_limit_retry % 10 == 0
          puts "Shopify Rate Limit Error Encountered"
        end

        if rate_limit_retry >= rate_limit_limit
          raise
        else
          rate_limit_retry += 1
          sleep(rate_limit_retry)
          retry
        end
      elsif e.class == ActiveResource::ClientError
        raise
      end

      if network_retry >= network_limit
        raise
      else
        network_retry += 1
        sleep(network_retry)
        retry
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shopify_api_extensions-0.1.3 lib/shopify_api_extensions/backoff.rb