lib/ruby_skynet/client.rb in ruby_skynet-1.3.0.alpha3 vs lib/ruby_skynet/client.rb in ruby_skynet-2.0.0.rc1
- old
+ new
@@ -6,12 +6,13 @@
# RPC calls to Skynet
# Skynet Service autodiscovery
#
module RubySkynet
class Client
- include Base
+ include Common
+ # Allows this instance of the client to use different versions or regions.
attr_reader :skynet_name, :skynet_version, :skynet_region
# Version of the Skynet service to use
# By default it will connect to the latest version
# Default: '*'
@@ -81,18 +82,24 @@
# Raises RubySkynet::SkynetException
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
-
- # Obtain list of servers implementing this service in order of priority
- servers = ::RubySkynet.service_registry.servers_for(skynet_name, skynet_version, skynet_region)
-
logger.tagged request_id do
logger.benchmark_info "Called Skynet Service: #{skynet_name}.#{method_name}" do
- Connection.with_connection(servers, connection_params) do |connection|
- connection.rpc_call(request_id, skynet_name, method_name, parameters)
+ 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
@@ -102,10 +109,10 @@
# #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)
+ if !result.nil? && result[:exception].nil? && !self.class.method_defined?(method)
self.class.send(:define_method, method) {|*args| call(method, *args)}
end
result
end