class Fluent::PluginHelper::RetryState::RetryStateMachine

Attributes

current[R]
next_time[R]
secondary_transition_at[R]
secondary_transition_steps[R]
start[R]
steps[R]
timeout_at[R]
title[R]

Public Class Methods

new(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold) click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 38
def initialize(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold)
  @title = title

  @start = current_time
  @steps = 0
  @next_time = nil # should be initialized for first retry by child class

  @timeout = timeout
  @timeout_at = @start + timeout
  @current = :primary

  if randomize_width < 0 || randomize_width > 0.5
    raise "BUG: randomize_width MUST be between 0 and 0.5"
  end

  @randomize = randomize
  @randomize_width = randomize_width

  if forever && secondary
    raise "BUG: forever and secondary are exclusive to each other"
  end

  @forever = forever
  @max_steps = max_steps

  @secondary = secondary
  @secondary_threshold = secondary_threshold
  if @secondary
    raise "BUG: secondary_transition_threshold MUST be between 0 and 1" if @secondary_threshold <= 0 || @secondary_threshold >= 1
    max_retry_timeout = timeout
    if max_steps
      timeout_by_max_steps = calc_max_retry_timeout(max_steps)
      max_retry_timeout = timeout_by_max_steps if timeout_by_max_steps < max_retry_timeout
    end
    @secondary_transition_at = @start + max_retry_timeout * @secondary_threshold
    @secondary_transition_steps = nil
  end
end

Public Instance Methods

calc_next_time() click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 87
def calc_next_time
  if @forever || !@secondary # primary
    naive = naive_next_time(@steps)
    if @forever
      naive
    elsif naive >= @timeout_at
      @timeout_at
    else
      naive
    end
  elsif @current == :primary && @secondary
    naive = naive_next_time(@steps)
    if naive >= @secondary_transition_at
      @secondary_transition_at
    else
      naive
    end
  elsif @current == :secondary
    naive = naive_next_time(@steps - @secondary_transition_steps + 1)
    if naive >= @timeout_at
      @timeout_at
    else
      naive
    end
  else
    raise "BUG: it's out of design"
  end
end
current_time() click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 77
def current_time
  Time.now
end
limit?() click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 134
def limit?
  if @forever
    false
  else
    @next_time >= @timeout_at || !!(@max_steps && @steps >= @max_steps)
  end
end
naive_next_time(retry_times) click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 116
def naive_next_time(retry_times)
  raise NotImplementedError
end
randomize(interval) click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 81
def randomize(interval)
  return interval unless @randomize

  interval + (interval * @randomize_width * (2 * rand - 1.0))
end
secondary?() click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 120
def secondary?
  @secondary && (@current == :secondary || current_time >= @secondary_transition_at)
end
step() click to toggle source
# File lib/fluent/plugin_helper/retry_state.rb, line 124
def step
  @steps += 1
  if @secondary && @current != :secondary && current_time >= @secondary_transition_at
    @current = :secondary
    @secondary_transition_steps = @steps
  end
  @next_time = calc_next_time
  nil
end