Class: Vx::Lib::Consumer::Rpc::RpcClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/lib/consumer/rpc.rb

Constant Summary collapse

REP =
"rep".freeze
REQ =
"req".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(consumer) ⇒ RpcClient

Returns a new instance of RpcClient.



71
72
73
74
75
76
77
# File 'lib/vx/lib/consumer/rpc.rb', line 71

def initialize(consumer)
  @consumer = consumer
  @consumed = false
  @await    = {}
  @mutex    = Mutex.new
  @wakeup   = Mutex.new
end

Instance Attribute Details

#consumerObject (readonly)

Returns the value of attribute consumer.



69
70
71
# File 'lib/vx/lib/consumer/rpc.rb', line 69

def consumer
  @consumer
end

Instance Method Details

#call(ns, method, params, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/vx/lib/consumer/rpc.rb', line 123

def call(ns, method, params, options = {})
  timeout     = options[:timeout] || 3
  routing_key = options[:routing_key] || "vx.rpc.#{ns}".freeze
  call_id     = SecureRandom.uuid
  cond        = ConditionVariable.new
  result      = nil

  message = {
    method: method.to_s,
    params: params,
    id:     call_id
  }

  with_queue do |q|

    consumer.session.with_pub_channel do |ch|
      exch = ch.exchange RPC_EXCHANGE_NAME

      env = {
        payload:     message,
        rpc:         REQ,
        exchange:    exch.name,
        consumer:    consumer.params.consumer_name,
        properties:  { routing_key: routing_key, correlation_id: call_id },
        channel:     ch.id
      }

      @mutex.synchronize { @await[call_id] = cond }

      consumer.with_middlewares :pub, env do
        exch.publish(
          message.to_json,
          routing_key:    routing_key,
          correlation_id: call_id,
          reply_to:       q.name,
          content_type:   JSON_CONTENT_TYPE
        )
      end

      @wakeup.synchronize{
        cond.wait(@wakeup, timeout)
      }
      @mutex.synchronize do
        _, payload = @await.delete(call_id)
        if payload
          result = payload[RPC_PAYLOAD_RESULT]
        else
          nil
        end
      end
    end
  end

  result

end

#cancelObject



189
190
191
192
193
194
195
# File 'lib/vx/lib/consumer/rpc.rb', line 189

def cancel
  if subscriber?
    @subscriber.cancel
    @subscriber = nil
    @consumed   = false
  end
end

#consumeObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vx/lib/consumer/rpc.rb', line 79

def consume
  return if @consumed

  ch = consumer.session.conn.create_channel
  consumer.session.assign_error_handlers_to_channel(ch)

  @q = ch.queue(RPC_EXCHANGE_NAME, exclusive: true)

  @subscriber =
    @q.subscribe do |delivery_info, properties, payload|
      handle_delivery ch, properties, payload
    end
  @consumed = true
end

#handle_delivery(ch, properties, payload) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/vx/lib/consumer/rpc.rb', line 94

def handle_delivery(ch, properties, payload)

  if payload
    payload = ::JSON.parse(payload)
  end

  env = {
    consumer:   consumer.params.consumer_name,
    queue:      @q.name,
    rpc:        REP,
    channel:    ch.id,
    payload:    payload,
    properties: properties
  }

  consumer.with_middlewares :sub, env do
    call_id = properties[:correlation_id]
    c = @mutex.synchronize{ @await.delete(call_id) }
    if c
      @mutex.synchronize do
        @await[call_id] = [properties, payload]
      end
      @wakeup.synchronize do
        c.signal
      end
    end
  end
end

#subscriber?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/vx/lib/consumer/rpc.rb', line 185

def subscriber?
  !!@subscriber
end

#with_queue {|@q| ... } ⇒ Object

Yields:

  • (@q)


180
181
182
183
# File 'lib/vx/lib/consumer/rpc.rb', line 180

def with_queue
  consume
  yield @q
end