Sha256: c5f3be453fd9fa9ca1423f818548c1fe6dbd88c8df18f8a106a6354b71006a74

Contents?: true

Size: 1.06 KB

Versions: 10

Compression:

Stored size: 1.06 KB

Contents

module Racecar
  class Pause
    def initialize(timeout: nil, max_timeout: nil, exponential_backoff: false)
      @started_at = nil
      @pauses = 0
      @timeout = timeout
      @max_timeout = max_timeout
      @exponential_backoff = exponential_backoff
    end

    def pause!
      @started_at = Time.now
      @ends_at = @started_at + backoff_interval unless @timeout.nil?
      @pauses += 1
    end

    def resume!
      @started_at = nil
      @ends_at = nil
    end

    def paused?
      !@started_at.nil?
    end

    def pause_duration
      if paused?
        Time.now - @started_at
      else
        0
      end
    end

    def expired?
      return false if @timeout.nil?
      return true unless @ends_at
      Time.now >= @ends_at
    end

    def reset!
      @pauses = 0
    end

    def backoff_interval
      return Float::INFINITY if @timeout.nil?

      backoff_factor = @exponential_backoff ? 2**@pauses : 1
      timeout = backoff_factor * @timeout

      timeout = @max_timeout if @max_timeout && timeout > @max_timeout

      timeout
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
racecar-2.0.0 lib/racecar/pause.rb
racecar-2.0.0.beta6 lib/racecar/pause.rb
racecar-2.0.0.beta5 lib/racecar/pause.rb
racecar-2.0.0.beta4 lib/racecar/pause.rb
racecar-2.0.0.beta3 lib/racecar/pause.rb
racecar-2.0.0.beta2 lib/racecar/pause.rb
racecar-2.0.0.beta1 lib/racecar/pause.rb
racecar-2.0.0.alpha3 lib/racecar/pause.rb
racecar-2.0.0.alpha2 lib/racecar/pause.rb
racecar-2.0.0.alpha1 lib/racecar/pause.rb