Class: Protobuf::Rpc::Connectors::EventMachine

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/rpc/connectors/eventmachine.rb

Instance Attribute Summary

Attributes inherited from Base

#complete_cb, #failure_cb, #options, #success_cb

Instance Method Summary collapse

Methods inherited from Base

#async?, #initialize

Constructor Details

This class inherits a constructor from Protobuf::Rpc::Connectors::Base

Instance Method Details

#ensure_cbObject

Returns a callable that ensures any errors will be returned to the client

If a failure callback was set, just use that as a direct assignment otherwise implement one here that simply throws an exception, since we don't want to swallow the black holes.



32
33
34
# File 'lib/protobuf/rpc/connectors/eventmachine.rb', line 32

def ensure_cb
  @ensure_cb ||= (@failure_cb || lambda { |error| raise '%s: %s' % [error.code.name, error.message] } )
end

#log_signatureObject



36
37
38
# File 'lib/protobuf/rpc/connectors/eventmachine.rb', line 36

def log_signature
  @log_signature ||= "client-#{self.class}"
end

#send_requestObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protobuf/rpc/connectors/eventmachine.rb', line 9

def send_request
  ensure_em_running do 
    f = Fiber.current

    EM.next_tick do
      log_debug "[#{log_signature}] Scheduling EventMachine client request to be created on next tick"
      cnxn = EMClient.connect(options, &ensure_cb)
      cnxn.on_success(&success_cb) if success_cb
      cnxn.on_failure(&ensure_cb)
      cnxn.on_complete { resume_fiber(f) } unless async?
      log_debug "[#{log_signature}] Connection scheduled"
    end

    async? ? true : set_timeout_and_validate_fiber
  end
end