Module: Ferrum::Utils::ElapsedTime

Defined in:
lib/ferrum/utils/elapsed_time.rb

Overview

A monotonic-clock helper for tracking elapsed time and checking timeouts, backed by Concurrent.monotonic_time.

Class Method Summary collapse

Class Method Details

.elapsed_time(start = nil) ⇒ Float

Returns the time elapsed since start (or since start/reset was called).

Parameters:

  • start (Float, nil) (defaults to: nil)

    Monotonic time to measure from, defaults to the stored start point.

Returns:

  • (Float)


38
39
40
# File 'lib/ferrum/utils/elapsed_time.rb', line 38

def elapsed_time(start = nil)
  monotonic_time - (start || @start)
end

.monotonic_timeFloat

Returns the current monotonic clock time in seconds.

Returns:

  • (Float)


47
48
49
# File 'lib/ferrum/utils/elapsed_time.rb', line 47

def monotonic_time
  Concurrent.monotonic_time
end

.resetFloat

Resets the start point to the current monotonic time.

Returns:

  • (Float)


26
27
28
# File 'lib/ferrum/utils/elapsed_time.rb', line 26

def reset
  @start = monotonic_time
end

.startFloat

Sets the start point to the current monotonic time unless already set.

Returns:

  • (Float)


17
18
19
# File 'lib/ferrum/utils/elapsed_time.rb', line 17

def start
  @start ||= monotonic_time
end

.timeout?(start, timeout) ⇒ Boolean

Whether more than timeout seconds have elapsed since start.

Parameters:

  • start (Float)

    Monotonic time to measure from.

  • timeout (Float, Integer)

    The timeout, in seconds.

Returns:

  • (Boolean)


62
63
64
# File 'lib/ferrum/utils/elapsed_time.rb', line 62

def timeout?(start, timeout)
  elapsed_time(start) > timeout
end