Class: RubySkynet::Client

Inherits:
Object
  • Object
show all
Includes:
SyncAttr
Defined in:
lib/ruby_skynet/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(service_name, version = '*', region = 'Development') ⇒ Client

Returns a new RubySkynet Client for the named service

Calls to an instance of the Client are thread-safe and can be called concurrently from multiple threads at the same time

Parameters:

:service_name
  Name of the service to look for and connect to on Skynet

:version
  Optional version number of the service in Skynet
  Default: '*' being the latest version of the service

:region
  Optional region for this service in Skynet
  Default: 'Development'

Example

require 'ruby_skynet'
SemanticLogger.default_level = :trace
SemanticLogger.appenders << SemanticLogger::Appender::File(STDOUT)

tutorial_service = RubySkynet::Client.new('TutorialService')
p tutorial_service.call('Add', :value => 5)


38
39
40
41
42
43
# File 'lib/ruby_skynet/client.rb', line 38

def initialize(service_name, version='*', region='Development')
  @service_name = service_name
  @logger       = SemanticLogger::Logger.new("#{self.class.name}: #{service_name}/#{version}/#{region}")
  @version      = version
  @region       = region
end

Instance Method Details

#call(method_name, parameters, connection_params = {}) ⇒ Object

Performs a synchronous call to the Skynet Service

Parameters:

method_name [String|Symbol]:
  Name of the method to call at the service
parameters [Hash]:
  Parameters to pass into the service

Returns the Hash result returned from the Skynet Service

Raises RubySkynet::ProtocolError Raises RubySkynet::SkynetException



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_skynet/client.rb', line 57

def call(method_name, parameters, connection_params={})
  # Skynet requires BSON RPC Calls to have the following format:
  # https://github.com/bketelsen/skynet/blob/protocol/protocol.md
  request_id = BSON::ObjectId.new.to_s
  @logger.tagged request_id do
    @logger.benchmark_info "Called Skynet Service: #{@service_name}.#{method_name}" do
      retries = 0
      # If it cannot connect to a server, try a different server
      begin
        Connection.with_connection(Registry.server_for(@service_name, @version, @region), connection_params) do |connection|
          connection.rpc_call(request_id, @service_name, method_name, parameters)
        end
      rescue ResilientSocket::ConnectionFailure => exc
        if (retries < 3) && exc.cause.is_a?(Errno::ECONNREFUSED)
          retries += 1
          retry
        end
        # TODO rescue ServiceUnavailable retry x times until the service becomes available
      end
    end
  end
end