class SSE::Impl::Backoff
A simple backoff algorithm that can reset itself after a given interval has passed without errors. A random jitter of up to -50% is applied to each interval.
Attributes
base_interval[RW]
The minimum value for the backoff interval.
Public Class Methods
new(base_interval, max_interval, reconnect_reset_interval: 60)
click to toggle source
Constructs a backoff counter.
@param [Float] base_interval
the minimum value @param [Float] max_interval the maximum value @param [Float] reconnect_reset_interval the interval will be reset to the minimum if this number of
seconds elapses between the last call to {#mark_success} and the next call to {#next_interval}
# File lib/ld-eventsource/impl/backoff.rb, line 17 def initialize(base_interval, max_interval, reconnect_reset_interval: 60) @base_interval = base_interval @max_interval = max_interval @reconnect_reset_interval = reconnect_reset_interval @attempts = 0 @last_good_time = nil @jitter_rand = Random.new end
Public Instance Methods
mark_success()
click to toggle source
Marks the current time as being the beginning of a valid connection state, resetting the timer that measures how long the state has been valid.
# File lib/ld-eventsource/impl/backoff.rb, line 51 def mark_success @last_good_time = Time.now.to_f end
next_interval()
click to toggle source
Computes the next interval value.
@return [Float] the next interval in seconds
# File lib/ld-eventsource/impl/backoff.rb, line 36 def next_interval if !@last_good_time.nil? good_duration = Time.now.to_f - @last_good_time @attempts = 0 if good_duration >= @reconnect_reset_interval end @last_good_time = nil target = ([@base_interval * (2 ** @attempts), @max_interval].min).to_f @attempts += 1 (target / 2) + @jitter_rand.rand(target / 2) end