lib/protobuf/nats.rb in protobuf-nats-0.9.0.pre2 vs lib/protobuf/nats.rb in protobuf-nats-0.9.0.pre3

- old
+ new

@@ -76,46 +76,43 @@ subscription_key = "rpc.#{service_class_name}.#{service_method_name}" subscription_key = config.make_subscription_key_replacements(subscription_key) end def self.start_client_nats_connection - @start_client_nats_connection ||= begin - GET_CONNECTED_MUTEX.synchronize do - break true if @client_nats_connection - break true if @start_client_nats_connection + return true if @start_client_nats_connection && @client_nats_connection - # Disable publisher pending buffer on reconnect - options = config.connection_options.merge(:disable_reconnect_buffer => true) + GET_CONNECTED_MUTEX.synchronize do + break true if @client_nats_connection + break true if @start_client_nats_connection - begin - @client_nats_connection = NatsClient.new - @client_nats_connection.connect(options) - rescue ::Protobuf::Nats::Errors::IOException - @client_nats_connection = nil - raise - end + # Disable publisher pending buffer on reconnect + options = config.connection_options.merge(:disable_reconnect_buffer => true) - # Ensure we have a valid connection to the NATS server. - @client_nats_connection.flush(5) + client = NatsClient.new + client.connect(options) - @client_nats_connection.on_disconnect do - logger.warn("Client NATS connection was disconnected") - end + # Ensure we have a valid connection to the NATS server. + client.flush(5) - @client_nats_connection.on_reconnect do - logger.warn("Client NATS connection was reconnected") - end + client.on_disconnect do + logger.warn("Client NATS connection was disconnected") + end - @client_nats_connection.on_close do - logger.warn("Client NATS connection was closed") - end + client.on_reconnect do + logger.warn("Client NATS connection was reconnected") + end - @client_nats_connection.on_error do |error| - notify_error_callbacks(error) - end + client.on_close do + logger.warn("Client NATS connection was closed") + end - true + client.on_error do |error| + notify_error_callbacks(error) end + + @client_nats_connection = client + + true end end # This will work with both ruby and java errors def self.log_error(error)