Sha256: 818fa38ad55a8bf9375e0ba3b52f831c7217b1b976741484bb4d124f39cbaae9

Contents?: true

Size: 862 Bytes

Versions: 8

Compression:

Stored size: 862 Bytes

Contents

require 'thread'
require 'timeout'

class Freddy
  class SyncResponseContainer
    def initialize
      @mutex = Mutex.new
    end

    def call(response, delivery)
      @response = response
      @delivery = delivery
      @mutex.synchronize { @waiting.wakeup }
    end

    def wait_for_response(timeout)
      @mutex.synchronize do
        @waiting = Thread.current
        @mutex.sleep(timeout)
      end

      if @response.nil?
        raise Timeout::Error, 'execution expired'
      elsif @response[:error] == 'RequestTimeout'
        raise TimeoutError.new(@response)
      elsif !@delivery || @delivery.type == 'error'
        raise InvalidRequestError.new(@response)
      else
        @response
      end
    end

    private

    def to_proc
      Proc.new {|*args| self.call(*args)}
    end

    def filled?
      !@response.nil?
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
freddy-jruby-0.5.1 lib/freddy/sync_response_container.rb
freddy-0.5.1 lib/freddy/sync_response_container.rb
freddy-jruby-0.5.0 lib/freddy/sync_response_container.rb
freddy-0.5.0 lib/freddy/sync_response_container.rb
freddy-jruby-0.4.9 lib/freddy/sync_response_container.rb
freddy-0.4.9 lib/freddy/sync_response_container.rb
freddy-0.4.8 lib/freddy/sync_response_container.rb
freddy-0.4.7 lib/freddy/sync_response_container.rb