Sha256: 57483eacf7a9644f57a05174aee26f7f8e47d7c24c97ef4b434dfb97457c2a10

Contents?: true

Size: 890 Bytes

Versions: 19

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true

require 'timeout'

class Freddy
  class SyncResponseContainer
    def initialize(on_timeout)
      @mutex = Mutex.new
      @resource = ConditionVariable.new
      @on_timeout = on_timeout
    end

    def call(response, delivery)
      raise StandardError, 'unexpected nil value for response' if response.nil?

      @response = response
      @delivery = delivery
      @mutex.synchronize { @resource.signal }
    end

    def wait_for_response(timeout)
      @mutex.synchronize { @response || @resource.wait(@mutex, timeout) }

      if !@response
        @on_timeout.call
        raise TimeoutError.new(
          error: 'RequestTimeout',
          message: 'Timed out waiting for response'
        )
      elsif !@delivery || @delivery.type == 'error'
        raise InvalidRequestError, @response
      else
        @response
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
freddy-2.8.0 lib/freddy/sync_response_container.rb
freddy-2.7.0 lib/freddy/sync_response_container.rb
freddy-2.6.0 lib/freddy/sync_response_container.rb
freddy-2.5.1 lib/freddy/sync_response_container.rb
freddy-2.5.0 lib/freddy/sync_response_container.rb
freddy-2.5.0.pre.rc.1 lib/freddy/sync_response_container.rb
freddy-2.4.0 lib/freddy/sync_response_container.rb
freddy-2.3.0 lib/freddy/sync_response_container.rb
freddy-2.2.4 lib/freddy/sync_response_container.rb
freddy-2.2.3 lib/freddy/sync_response_container.rb
freddy-2.2.2 lib/freddy/sync_response_container.rb
freddy-2.2.1 lib/freddy/sync_response_container.rb
freddy-2.2.0 lib/freddy/sync_response_container.rb
freddy-2.1.0 lib/freddy/sync_response_container.rb
freddy-2.0.0 lib/freddy/sync_response_container.rb
freddy-1.7.0 lib/freddy/sync_response_container.rb
freddy-1.6.0 lib/freddy/sync_response_container.rb
freddy-1.5.0 lib/freddy/sync_response_container.rb
freddy-1.4.2 lib/freddy/sync_response_container.rb