Sha256: 4d38379ea4000c9919a5393b86a8bca83141849aeec2a5a4185cb80496634fff

Contents?: true

Size: 745 Bytes

Versions: 2

Compression:

Stored size: 745 Bytes

Contents

# frozen_string_literal: true

require 'timeout'

# Timeout extensions
module ::Timeout
  # Sets a timeout for the given block. This method provides an equivalent API
  # to the stock Timeout API provided by Ruby. In case of a timeout, the block
  # will be interrupted and an exception will be raised according to the given
  # arguments.
  #
  # @param sec [Number] timeout period in seconds
  # @param klass [Class] exception class
  # @param message [String] exception message
  # @return [any] block's return value
  def self.timeout(sec, klass = Timeout::Error, message = 'execution expired', &block)
    if sec.nil? || sec == 0
      block.call
    else
      cancel_after(sec, with_exception: [klass, message], &block)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polyphony-1.6 lib/polyphony/extensions/timeout.rb
polyphony-1.5 lib/polyphony/extensions/timeout.rb