Sha256: 00e2769ae57c81b130bf017584b87b9a869752ff5f519f33342d952638f0a86d

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module V1
        class Factorial
          module Utils
            module Timeout
              class WithTimeout < ConvenientService::Command
                ReturnStruct = ::Struct.new(:timeout?, :value, keyword_init: true)

                attr_reader :seconds, :block

                def initialize(seconds, &block)
                  @seconds = seconds
                  @block = block
                end

                ##
                # @internal
                #   NOTE: Prefer to avoid `Timeout.timeout` if you are NOT absolutely 100% sure what you are doing. See the following articles for details.
                #   - https://github.com/ruby/timeout/blob/master/lib/timeout.rb
                #   - https://adamhooper.medium.com/in-ruby-dont-use-timeout-77d9d4e5a001
                #   - https://flushentitypacket.github.io/ruby/2015/02/21/ruby-timeout-how-does-it-even-work.html
                #   - https://www.exceptionalcreatures.com/bestiary/Timeout/Error
                #   - https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts#regexp
                #
                def call
                  return struct(timeout?: false, value: block.call) if seconds.nil?
                  return struct(timeout?: true, value: nil) if seconds.zero?

                  begin
                    struct(timeout?: false, value: ::Timeout.timeout(seconds, &block))
                  rescue ::Timeout::Error
                    struct(timeout?: true, value: nil)
                  end
                end

                private

                def struct(**kwargs)
                  ReturnStruct.new(**kwargs)
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.17.0 lib/convenient_service/examples/standard/v1/factorial/utils/timeout/with_timeout.rb
convenient_service-0.16.0 lib/convenient_service/examples/standard/v1/factorial/utils/timeout/with_timeout.rb
convenient_service-0.15.0 lib/convenient_service/examples/standard/v1/factorial/utils/timeout/with_timeout.rb
convenient_service-0.14.0 lib/convenient_service/examples/standard/v1/factorial/utils/timeout/with_timeout.rb