Method: NetSuite::Utilities#backoff

Defined in:
lib/netsuite/utilities.rb

#backoff(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/netsuite/utilities.rb', line 40

def backoff(options = {})
  # TODO the default backoff attempts should be customizable the global config
  options[:attempts] ||= 8

  count = 0

  begin
    count += 1
    yield
  rescue Exception => e
    exceptions_to_retry = [
      Errno::ECONNRESET,
      Errno::ETIMEDOUT,
      Errno::EHOSTUNREACH,
      EOFError,
      Wasabi::Resolver::HTTPError,
      Savon::SOAPFault,
      Savon::InvalidResponseError,
      Zlib::BufError,
      Savon::HTTPError,
      SocketError,
      Net::OpenTimeout
    ]

    # available in ruby > 1.9
    if defined?(Net::ReadTimeout)
      exceptions_to_retry << Net::ReadTimeout
    end

    # available in ruby > 2.2.0
    exceptions_to_retry << IO::EINPROGRESSWaitWritable if defined?(IO::EINPROGRESSWaitWritable)
    exceptions_to_retry << OpenSSL::SSL::SSLErrorWaitReadable if defined?(OpenSSL::SSL::SSLErrorWaitReadable)

    # depends on the http library chosen
    exceptions_to_retry << Excon::Error::Timeout if defined?(Excon::Error::Timeout)
    exceptions_to_retry << Excon::Error::Socket if defined?(Excon::Error::Socket)

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

    # whitelist certain SOAPFaults; all other network errors should automatically retry
    if e.is_a?(Savon::SOAPFault)
      # https://github.com/stripe/stripe-netsuite/issues/815
      if !e.message.include?("Only one request may be made against a session at a time") &&
        !e.message.include?('java.util.ConcurrentModificationException') &&
        !e.message.include?('com.netledger.common.exceptions.NLDatabaseOfflineException') &&
        !e.message.include?('com.netledger.database.NLConnectionUtil$NoCompanyDbsOnlineException') &&
        !e.message.include?('com.netledger.cache.CacheUnavailableException') &&
        !e.message.include?('An unexpected error occurred.') &&
        !e.message.include?('Session invalidation is in progress with different thread') &&
        !e.message.include?('SuiteTalk concurrent request limit exceeded. Request blocked.') &&
        !e.message.include?('The Connection Pool is not intialized.') &&
        # it looks like NetSuite mispelled their error message...
        !e.message.include?('The Connection Pool is not intiialized.')
        raise
      end
    end

    if count >= options[:attempts]
      raise
    end

    # log.warn("concurrent request failure", sleep: count, attempt: count)
    sleep(count)

    retry
  end
end