class PowerTrack::Retrier

A utility class that manges an exponential backoff retry pattern. Additionally, this king of retrier can be reset or stopped by the code being retried.

Constants

DEFAULT_INTERVAL_MULTIPLIER

the default interval multiplier

DEFAULT_MAX_ELAPSED_TIME

the default maximum number of seconds to wait b/w 2 attempts

DEFAULT_MIN_INTERVAL

the default minimum number of seconds b/w 2 attempts

DEFAULT_OPTIONS

default options used by a retrier unless others specified at initialization

DEFAULT_RANDOMIZE_FACTOR

the default randomize factor

Attributes

max_retries[R]
retries[R]

Public Class Methods

new(max_retries, options=nil) click to toggle source

Builds a retrier that will retry a maximum retries number of times.

# File lib/powertrack/streaming/retrier.rb, line 28
def initialize(max_retries, options=nil)
  options = DEFAULT_OPTIONS.merge(options || {})

  @max_retries = max_retries
  @retries = 0
  @continue = true
  @backoff = ExponentialBackoff.new(options[:min_interval], options[:max_elapsed_time])
  @backoff.multiplier = options[:multiplier]
  @backoff.randomize_factor = options[:randomize_factor]
end

Public Instance Methods

reset!() click to toggle source

Resets the retrier.

# File lib/powertrack/streaming/retrier.rb, line 40
def reset!
  @retries = 0
  @backoff.clear
end
retry() { || ... } click to toggle source

Retries the block of code provided according to the configuration of the retrier.

# File lib/powertrack/streaming/retrier.rb, line 57
def retry(&block)
  # TODO: manage exceptions
  while @continue && @retries <= @max_retries
    res = yield
    if @continue
      @retries += 1
      sleep(@backoff.next_interval)
    end
  end

  res
end
retrying?() click to toggle source

Returns true if the retrier is currently retrying.

# File lib/powertrack/streaming/retrier.rb, line 46
def retrying?
  @retries != 0
end
stop() click to toggle source

Stops retrying even after a reset. To be used from the code being retried.

# File lib/powertrack/streaming/retrier.rb, line 51
def stop
  @continue = false
end