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