Module: BoomNats::Requester

Included in:
BoomNats
Defined in:
lib/boom_nats/requester.rb

Instance Method Summary collapse

Instance Method Details

#request(topic, params = nil, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/boom_nats/requester.rb', line 3

def request(topic, params = nil, options = {})
  result = nil
  BoomNats.application.execute do |nats|
    timeout = Concurrent::Cancellation.timeout 5
    done = Concurrent::Channel.new(capacity: 1)
    Concurrent::Channel.go do
      nats.request(topic, params.to_json, options) do |msg|
        done << JSON.parse(msg)
      end
      timeout.origin.wait
      done << BoomNats::Error.new("request do not received")
    end
    result = ~done # block until signaled
  end
  raise result if result.is_a?(BoomNats::Error)

  result
end