Class: Isono::NodeModules::RpcChannel
- 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
-
#amq ⇒ Object
readonly
Returns the value of attribute amq.
Attributes inherited from Base
Instance Method Summary collapse
-
#register_endpoint(endpoint, app, opts = {}) ⇒ Object
Register a new RPC endpoint.
-
#request(endpoint, command, *args, &blk) ⇒ RequestContext, any
Make a RPC request to an endpoint.
-
#unregister_endpoint(endpoint) ⇒ Object
Unregister endpoint.
Methods included from Logger
Methods inherited from Base
#config_section, #initialize, #manifest, #value_object
Constructor Details
This class inherits a constructor from Isono::NodeModules::Base
Instance Attribute Details
#amq ⇒ Object (readonly)
Returns the value of attribute amq.
71 72 73 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 71 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.
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 179 180 181 182 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 131 def register_endpoint(endpoint, app, opts={}) raise TypeError unless app.respond_to?(:call) opts = {:exclusive=>true, :prefetch=>0}.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. }.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 = if opts[:prefetch].to_i > 0 # create per endpoint channel node.create_channel else # use default channel @amq end ch.instance_eval %Q{ def endpoint_queue self.queue("isono.rpc.endpoint.#{endpoint}", {:exclusive=>false, :auto_delete=>true}) end def response_exchange self.direct('') end } ch.prefetch(opts[:prefetch].to_i) if opts[:prefetch].to_i > 0 # stores hash here that is for thread safety. @endpoints[endpoint]={:app=>app, :opts=>opts, :ch=>ch} ch.endpoint_queue.subscribe(:ack=>true, &endpoint_proc) event.publish('rpc/register', :args=>[endpoint]) } end |
#request(endpoint, command, *args, &blk) ⇒ RequestContext, any
Make a RPC request to an endpoint.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 106 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.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/isono/node_modules/rpc_channel.rb', line 187 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 |