Class: Isono::NodeModules::RpcChannel

Inherits:
Base
  • Object
show all
Includes:
Logger
Defined in:
lib/isono/node_modules/rpc_channel.rb

Defined Under Namespace

Classes: DuplicateEndpointError, OneshotResponseContext, RequestContext, ResponseContext, RpcError, UnknownEndpointError

Instance Attribute Summary collapse

Attributes inherited from Base

#node

Instance Method Summary collapse

Methods included from Logger

included, initialize

Methods inherited from Base

#config_section, #initialize, #manifest, #value_object

Constructor Details

This class inherits a constructor from Isono::NodeModules::Base

Instance Attribute Details

#amqObject (readonly)

Returns the value of attribute amq.



105
106
107
# File 'lib/isono/node_modules/rpc_channel.rb', line 105

def amq
  @amq
end

Instance Method Details

#register_endpoint(endpoint, app, opts = {}) ⇒ Object

Register a new RPC endpoint.

This method works in sync mode if called at non-EM reactor thread.

Parameters:

  • endpoint (String)
  • app (has call() method)
  • opts (Hash) (defaults to: {})

    :exclusive :prefetch

Raises:

  • (TypeError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/isono/node_modules/rpc_channel.rb', line 165

def register_endpoint(endpoint, app, opts={})
  raise TypeError unless app.respond_to?(:call)
  opts = {:exclusive=>true, :prefetch=>1}.merge(opts)

  # create receive queue for new RPC endpoint.
  endpoint_proc = proc { |header, data|

    data = Serializer.instance.unmarshal(data)

    resctx = if data[:oneshot]
               OneshotResponseContext.new(@endpoints[endpoint][:ch].response_exchange, header)
             else
               ResponseContext.new(@endpoints[endpoint][:ch].response_exchange, header)
             end
    begin
      req = Rack::Request.new({:sender=>header.reply_to['command-recv.'.size..-1],
                                :message_id=>header.message_id
                              }.merge(data))
      res = Rack::Response.new(resctx)
      ret = app.call(req, res)
    rescue ::Exception => e
      logger.error(e)
      resctx.response(e) unless resctx.responded?
    end
  }


  EventMachine.schedule {
    ch = node.create_channel

    ch.instance_eval %Q{
      def endpoint_queue
        self.queue("isono.rpc.endpoint.#{endpoint}", {:exclusive=>false, :auto_delete=>true})
      end

      def response_exchange
        self.default_exchange
      end
    }
    ch.prefetch(opts[:prefetch].to_i) if opts[:prefetch].to_i > 0
    # stores hash here that is for thread safety.
    if !@endpoints.nil?
      @endpoints[endpoint]={:app=>app, :opts=>opts, :ch=>ch}

      ch.endpoint_queue.subscribe(:ack=>true, &endpoint_proc)
      event.publish('rpc/register', :args=>[endpoint])
    else
      logger.error("No such endpoints")
    end
  }
end

#request(endpoint, command, *args, &blk) ⇒ RequestContext, any

Make a RPC request to an endpoint.

Examples:

create a sync RPC request.

rpc.request('endpoint1', 'func1', xxxx)

call RPC in async mode.

rpc.request('endpoint1', 'func1', xxxx) { |req|
  req.on_success { |r|
    puts r
  }
  req.on_error { |r|
    puts r
  }
}

setup request context and do wait().

Note that callbacks are
rpc.request('endpoint1', 'func1', xxxx) { |req|
  # send new attribute
  req.request[:xxxx] = "sdfsdf"
  # returns synchronized RequestContext to block caller.
  req.synchronize
}.wait # request() get back the altered RequestCotenxt that has wait().

Create async oneshot call. (do not expect response)

rpc.request('endpoint1', 'func1') { |req|
  req.oneshot = true
}

Parameters:

  • endpoint (String)
  • command (String)
  • args (Array)
  • &blk (Proc)

    Block to setup the request context.

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/isono/node_modules/rpc_channel.rb', line 140

def request(endpoint, command, *args, &blk)
  req = RequestContext.new(endpoint, command, args)
  # the block is to setup the request context prior to sending.
  if blk
    # async
    r = blk.call(req)
    req = r if r.is_a?(RequestContext)
    send_request(req)
    req
  else
    # sync
    req = req.synchronize
    send_request(req)
    req.wait
  end
end

#unregister_endpoint(endpoint) ⇒ Object

Unregister endpoint.

Parameters:

  • endpoint (String)

    endpoint name to be removed



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/isono/node_modules/rpc_channel.rb', line 220

def unregister_endpoint(endpoint)
  if @endpoints.has_key?(endpoint)
    EventMachine.schedule {
      data = @endpoints.delete(endpoint)
      # endpoint_queue is :auto_delete=>true so that it will be deleted
      # in case of zero consumers.
      data[:ch].endpoint_queue.unsubscribe
      event.publish('rpc/unregister', :args=>[endpoint])
    }
  end
end