Class: Fluent::PluginHelper::RetryState::RetryStateMachine
- Inherits:
-
Object
- Object
- Fluent::PluginHelper::RetryState::RetryStateMachine
- Defined in:
- lib/fluent/plugin_helper/retry_state.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#next_time ⇒ Object
readonly
Returns the value of attribute next_time.
-
#secondary_transition_at ⇒ Object
readonly
Returns the value of attribute secondary_transition_at.
-
#secondary_transition_steps ⇒ Object
readonly
Returns the value of attribute secondary_transition_steps.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#steps ⇒ Object
readonly
Returns the value of attribute steps.
-
#timeout_at ⇒ Object
readonly
Returns the value of attribute timeout_at.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #calc_next_time ⇒ Object
- #current_time ⇒ Object
-
#initialize(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold) ⇒ RetryStateMachine
constructor
A new instance of RetryStateMachine.
- #limit? ⇒ Boolean
- #naive_next_time(retry_times) ⇒ Object
- #randomize(interval) ⇒ Object
- #recalc_next_time ⇒ Object
- #secondary? ⇒ Boolean
- #step ⇒ Object
Constructor Details
#initialize(title, wait, timeout, forever, max_steps, randomize, randomize_width, secondary, secondary_threshold) ⇒ RetryStateMachine
Returns a new instance of RetryStateMachine.
38 39 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 |
# 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 @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 |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def current @current end |
#next_time ⇒ Object (readonly)
Returns the value of attribute next_time.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def next_time @next_time end |
#secondary_transition_at ⇒ Object (readonly)
Returns the value of attribute secondary_transition_at.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def secondary_transition_at @secondary_transition_at end |
#secondary_transition_steps ⇒ Object (readonly)
Returns the value of attribute secondary_transition_steps.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def secondary_transition_steps @secondary_transition_steps end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def start @start end |
#steps ⇒ Object (readonly)
Returns the value of attribute steps.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def steps @steps end |
#timeout_at ⇒ Object (readonly)
Returns the value of attribute timeout_at.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def timeout_at @timeout_at end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
36 37 38 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 36 def title @title end |
Instance Method Details
#calc_next_time ⇒ Object
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 109 110 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 83 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 ⇒ Object
73 74 75 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 73 def current_time Time.now end |
#limit? ⇒ Boolean
134 135 136 137 138 139 140 |
# 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) ⇒ Object
112 113 114 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 112 def naive_next_time(retry_times) raise NotImplementedError end |
#randomize(interval) ⇒ Object
77 78 79 80 81 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 77 def randomize(interval) return interval unless @randomize interval + (interval * @randomize_width * (2 * rand - 1.0)) end |
#recalc_next_time ⇒ Object
130 131 132 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 130 def recalc_next_time @next_time = calc_next_time end |
#secondary? ⇒ Boolean
116 117 118 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 116 def secondary? !@forever && @secondary && (@current == :secondary || current_time >= @secondary_transition_at) end |
#step ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/fluent/plugin_helper/retry_state.rb', line 120 def step @steps += 1 if !@forever && @secondary && @current != :secondary && current_time >= @secondary_transition_at @current = :secondary @secondary_transition_steps = @steps end @next_time = calc_next_time nil end |