Sha256: 582d1af8605620f3da68fea627353059d0e7bfcf19bc4d7d7f6a1f9ddb4f6b9b

Contents?: true

Size: 1.14 KB

Versions: 27

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Racecar
  class Pause
    attr_reader :pauses_count

    def initialize(timeout: nil, max_timeout: nil, exponential_backoff: false)
      @started_at = nil
      @pauses_count = 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_count += 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_count = 0
    end

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

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

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

      timeout
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
racecar-2.11.0 lib/racecar/pause.rb
racecar-2.11.0.beta4 lib/racecar/pause.rb
racecar-2.11.0.beta3 lib/racecar/pause.rb
racecar-2.11.0.beta2 lib/racecar/pause.rb
racecar-2.11.0.beta1 lib/racecar/pause.rb
racecar-2.10.0 lib/racecar/pause.rb
racecar-2.10.0.beta2 lib/racecar/pause.rb
racecar-2.10.0.beta1 lib/racecar/pause.rb
racecar-2.9.0 lib/racecar/pause.rb
racecar-2.9.0.beta1 lib/racecar/pause.rb
racecar-2.8.2 lib/racecar/pause.rb
racecar-2.8.2.beta lib/racecar/pause.rb
racecar-2.8.1 lib/racecar/pause.rb
racecar-2.8.0.rc.1 lib/racecar/pause.rb
racecar-2.8.0.beta.2 lib/racecar/pause.rb
racecar-2.8.0.beta.1 lib/racecar/pause.rb
racecar-2.7.0 lib/racecar/pause.rb
racecar-2.7.0.beta1 lib/racecar/pause.rb
racecar-2.6.0 lib/racecar/pause.rb
racecar-2.5.0 lib/racecar/pause.rb