Class: Nonnative::Timeout

Inherits:
Object
  • Object
show all
Defined in:
lib/nonnative/timeout.rb

Overview

Small helper to run a block with a timeout and convert timeout errors into false.

This is used internally for readiness/shutdown loops (for example port checks) where the common control-flow is “keep retrying until the timeout elapses”.

Examples:

timeout = Nonnative::Timeout.new(1) # seconds
ok = timeout.perform do
  # do work that may take time
  true
end
# ok is either the block result or false if the timeout elapsed

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ Timeout

Returns a new instance of Timeout.

Parameters:

  • time (Numeric)

    timeout duration in seconds



19
20
21
# File 'lib/nonnative/timeout.rb', line 19

def initialize(time)
  @time = time
end

Instance Method Details

#perform { ... } ⇒ Object, false

Executes the given block with the configured timeout.

If the timeout elapses, returns false instead of raising Timeout::Error.

Yields:

  • the work to execute under a timeout

Returns:

  • (Object, false)

    the block’s return value, or false if the timeout elapsed



29
30
31
32
33
# File 'lib/nonnative/timeout.rb', line 29

def perform(&)
  ::Timeout.timeout(time, &)
rescue ::Timeout::Error
  false
end