Class: RubySkynet::Client
- Inherits:
-
Object
- Object
- RubySkynet::Client
- Includes:
- Base
- Defined in:
- lib/ruby_skynet/client.rb
Instance Attribute Summary collapse
-
#skynet_name ⇒ Object
readonly
Returns the value of attribute skynet_name.
-
#skynet_region ⇒ Object
readonly
Returns the value of attribute skynet_region.
-
#skynet_version ⇒ Object
readonly
Returns the value of attribute skynet_version.
Class Method Summary collapse
-
.skynet_version ⇒ Object
Version of the Skynet service to use By default it will connect to the latest version Default: ‘*’.
Instance Method Summary collapse
-
#call(method_name, parameters, connection_params = {}) ⇒ Object
Performs a synchronous call to the Skynet Service.
-
#initialize(skynet_name = self.class.skynet_name, skynet_version = self.class.skynet_version, skynet_region = self.class.skynet_region) ⇒ Client
constructor
Returns a new RubySkynet Client for the named service.
-
#method_missing(method, *args, &block) ⇒ Object
Implement methods that call the remote Service.
Methods included from Base
Constructor Details
#initialize(skynet_name = self.class.skynet_name, skynet_version = self.class.skynet_version, skynet_region = self.class.skynet_region) ⇒ 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:
:skynet_name
Only required when creating instance of RubySkynet::Client directly
Otherwise it defaults to the name of the class
Name of the service to look for and connect to on Skynet
:skynet_version
Optional version number of the service in Skynet
Default: '*' being the latest version of the service
:skynet_region
Optional region for this service in Skynet
Default: RubySkynet.region
Example using Client class
require 'ruby_skynet'
SemanticLogger.default_level = :info
SemanticLogger.add_appender(STDOUT)
class EchoService < RubySkynet::Client
end
echo_service = EchoService.new
p echo_service.echo(:value => 5)
Example using Ruby Client directly
require 'ruby_skynet'
SemanticLogger.default_level = :info
SemanticLogger.add_appender(STDOUT)
tutorial_service = RubySkynet::Client.new('TutorialService')
p tutorial_service.call('Add', :value => 5)
61 62 63 64 65 66 67 68 |
# File 'lib/ruby_skynet/client.rb', line 61 def initialize(skynet_name=self.class.skynet_name, skynet_version=self.class.skynet_version, skynet_region=self.class.skynet_region) @skynet_name = skynet_name @skynet_version = skynet_version @skynet_region = skynet_region self.logger = SemanticLogger["#{self.class.name}: #{@skynet_name}/#{@skynet_version}/#{@skynet_region}"] raise "skynet_name is mandatory when using RubySkynet::Client directly" if @skynet_name == RubySkynet::Client.name end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Implement methods that call the remote Service
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ruby_skynet/client.rb', line 106 def method_missing(method, *args, &block) result = call(method, *args) # #TODO if Service returns method undefined, call super # # Define the method if the call was successful and no other thread has # already created the method if result[:exception].nil? && !self.class.method_defined?(method) self.class.send(:define_method, method) {|*args| call(method, *args)} end result end |
Instance Attribute Details
#skynet_name ⇒ Object (readonly)
Returns the value of attribute skynet_name.
13 14 15 |
# File 'lib/ruby_skynet/client.rb', line 13 def skynet_name @skynet_name end |
#skynet_region ⇒ Object (readonly)
Returns the value of attribute skynet_region.
13 14 15 |
# File 'lib/ruby_skynet/client.rb', line 13 def skynet_region @skynet_region end |
#skynet_version ⇒ Object (readonly)
Returns the value of attribute skynet_version.
13 14 15 |
# File 'lib/ruby_skynet/client.rb', line 13 def skynet_version @skynet_version end |
Class Method Details
.skynet_version ⇒ Object
Version of the Skynet service to use By default it will connect to the latest version Default: ‘*’
18 19 20 |
# File 'lib/ruby_skynet/client.rb', line 18 def self.skynet_version @skynet_version ||= '*' 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ruby_skynet/client.rb', line 82 def call(method_name, parameters, connection_params={}) # Skynet requires BSON RPC Calls to have the following format: # https://github.com/skynetservices/skynet/blob/master/protocol.md request_id = BSON::ObjectId.new.to_s logger.tagged request_id do logger.benchmark_info "Called Skynet Service: #{skynet_name}.#{method_name}" do retries = 0 # If it cannot connect to a server, try a different server begin Connection.with_connection(::RubySkynet.service_registry.server_for(skynet_name, skynet_version, skynet_region), connection_params) do |connection| connection.rpc_call(request_id, skynet_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 |