Class: RudderAnalyticsSync::Retry

Inherits:
Object
  • Object
show all
Defined in:
lib/rudder_analytics_sync/retry.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =
3
DEFAULT_BASE_DELAY =
0.1
DEFAULT_MAX_DELAY =
30.0
DEFAULT_JITTER_RATIO =
0.2
RETRYABLE_ERRORS =
[
  Timeout::Error,
  EOFError,
  IOError,
  SocketError,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::ENETUNREACH,
  Errno::ETIMEDOUT,
  Net::OpenTimeout,
  Net::ReadTimeout,
  Net::ProtocolError,
  OpenSSL::SSL::SSLError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, clock: proc { Time.now }, random: proc { rand }) ⇒ Retry

Returns a new instance of Retry.



31
32
33
34
35
# File 'lib/rudder_analytics_sync/retry.rb', line 31

def initialize(config, clock: proc { Time.now }, random: proc { rand })
  @config = config
  @clock = clock
  @random = random
end

Instance Method Details

#delay(retry_number, response = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rudder_analytics_sync/retry.rb', line 58

def delay(retry_number, response = nil)
  backoff = @config.retry_base_delay * (2**(retry_number - 1))
  retry_after = retry_after_delay(response)
  base = [[backoff, retry_after].max, @config.max_retry_delay].min
  jittered = base + (base * @config.retry_jitter_ratio * @random.call)
  [jittered, @config.max_retry_delay].min
end

#enabled? ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rudder_analytics_sync/retry.rb', line 37

def enabled?
  @config.retry_enabled && @config.max_retries.positive?
end

#max_retries ⇒ Object



41
42
43
# File 'lib/rudder_analytics_sync/retry.rb', line 41

def max_retries
  @config.max_retries
end

#parse_retry_after(value) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/rudder_analytics_sync/retry.rb', line 66

def parse_retry_after(value)
  value = value.to_s.strip
  return value.to_i if value.match?(/\A\d+\z/)

  [Time.httpdate(value) - @clock.call, 0].max
rescue ArgumentError
  nil
end

#retries_remaining?(completed_retries) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rudder_analytics_sync/retry.rb', line 45

def retries_remaining?(completed_retries)
  completed_retries < @config.max_retries
end

#retryable_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rudder_analytics_sync/retry.rb', line 54

def retryable_error?(error)
  RETRYABLE_ERRORS.any? { |error_class| error.is_a?(error_class) }
end

#retryable_status?(status_code) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/rudder_analytics_sync/retry.rb', line 49

def retryable_status?(status_code)
  status_code = status_code.to_i
  status_code == 429 || (status_code >= 500 && status_code <= 599)
end