Class: Tep::Proxy::RetryPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRetryPolicy

Returns a new instance of RetryPolicy.



70
71
72
73
74
75
76
77
# File 'lib/tep/proxy.rb', line 70

def initialize
  @max_attempts        = 1
  @base_backoff_ms     = 0
  @backoff_multiplier  = 2
  # Default: transient upstream errors (gateway / unavailable /
  # timeout). 502 also catches our own connect-failure mapping.
  @retry_on_status = [502, 503, 504]
end

Instance Attribute Details

#backoff_multiplierObject

Returns the value of attribute backoff_multiplier.



67
68
69
# File 'lib/tep/proxy.rb', line 67

def backoff_multiplier
  @backoff_multiplier
end

#base_backoff_msObject

Returns the value of attribute base_backoff_ms.



67
68
69
# File 'lib/tep/proxy.rb', line 67

def base_backoff_ms
  @base_backoff_ms
end

#max_attemptsObject

Returns the value of attribute max_attempts.



67
68
69
# File 'lib/tep/proxy.rb', line 67

def max_attempts
  @max_attempts
end

#retry_on_statusObject

Returns the value of attribute retry_on_status.



68
69
70
# File 'lib/tep/proxy.rb', line 68

def retry_on_status
  @retry_on_status
end

Instance Method Details

#backoff_for(attempt) ⇒ Object

Milliseconds to sleep BEFORE attempt N (0-indexed). attempt=0 is the first retry’s pre-delay; attempt=1 the second’s, etc. Returns 0 when base is 0 (test-friendly: no delay between retries by default).



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tep/proxy.rb', line 97

def backoff_for(attempt)
  if @base_backoff_ms <= 0
    return 0
  end
  d = @base_backoff_ms
  i = 0
  while i < attempt
    d = d * @backoff_multiplier
    i += 1
  end
  d
end

#base_backoff_secsObject

Reader symmetric to the setter (Float seconds derived from the stored ms). Cheap; only the setter does the conversion in the common case.



89
90
91
# File 'lib/tep/proxy.rb', line 89

def base_backoff_secs
  @base_backoff_ms.to_f / 1000.0
end

#base_backoff_secs=(f) ⇒ Object

Float-seconds convenience setter (e.g. 0.5 -> 500ms). Stores the value in @base_backoff_ms as an int so backoff_for / the sleep call stay int-only on the hot path.



82
83
84
# File 'lib/tep/proxy.rb', line 82

def base_backoff_secs=(f)
  @base_backoff_ms = (f * 1000.0).to_i
end

#retriable?(status) ⇒ Boolean

Should the proxy retry given the upstream response status? Connect/send failures (status == 0) always count as retriable.

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tep/proxy.rb', line 112

def retriable?(status)
  if status == 0
    return true
  end
  i = 0
  while i < @retry_on_status.length
    if @retry_on_status[i] == status
      return true
    end
    i += 1
  end
  false
end