Class: SneakersPacker::RpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sneakers_packer/rpc_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher) ⇒ RpcClient

Returns a new instance of RpcClient.



8
9
10
11
12
# File 'lib/sneakers_packer/rpc_client.rb', line 8

def initialize(publisher)
  @publisher = publisher
  channel, exchange = fetch_channel_and_exchange
  @consumer = build_reply_queue(channel, exchange)
end

Instance Attribute Details

#call_idObject

Returns the value of attribute call_id.



5
6
7
# File 'lib/sneakers_packer/rpc_client.rb', line 5

def call_id
  @call_id
end

#conditionObject (readonly)

Returns the value of attribute condition.



6
7
8
# File 'lib/sneakers_packer/rpc_client.rb', line 6

def condition
  @condition
end

#lockObject (readonly)

Returns the value of attribute lock.



6
7
8
# File 'lib/sneakers_packer/rpc_client.rb', line 6

def lock
  @lock
end

#reply_queueObject (readonly)

Returns the value of attribute reply_queue.



4
5
6
# File 'lib/sneakers_packer/rpc_client.rb', line 4

def reply_queue
  @reply_queue
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sneakers_packer/rpc_client.rb', line 5

def response
  @response
end

Instance Method Details

#call(name, message, options = {}) ⇒ Object

call remote service via rabbitmq rpc

Parameters:

  • name

    route_key for service

  • message
  • options (timeout) (defaults to: {})
    int

    timeout. seconds. optional

Returns:

  • result of service

Raises:

  • RemoteCallTimeoutError if timeout



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sneakers_packer/rpc_client.rb', line 20

def call(name, message, options = {})
  self.call_id = SecureRandom.uuid
  self.response = nil

  ensure_reply_queue!

  @exchange.publish(message.to_s,
                    routing_key:    name.to_s,
                    correlation_id: call_id,
                    reply_to:       @reply_queue.name)

  timeout = (options[:timeout] || SneakersPacker.conf.rpc_timeout).to_i

  lock.synchronize { condition.wait(lock, timeout) }

  if response
    response
  else
    raise RemoteCallTimeoutError.new("远程调用超时")
  end
end