lib/protobuf/nats.rb in protobuf-nats-0.2.3 vs lib/protobuf/nats.rb in protobuf-nats-0.3.0

- old
+ new

@@ -4,10 +4,11 @@ # We don't need this, but the CLI attempts to terminate. require "protobuf/rpc/service_directory" require "nats/io/client" +require "protobuf/nats/errors" require "protobuf/nats/client" require "protobuf/nats/server" require "protobuf/nats/runner" require "protobuf/nats/config" @@ -51,14 +52,34 @@ @start_client_nats_connection ||= begin GET_CONNECTED_MUTEX.synchronize do break true if @client_nats_connection break true if @start_client_nats_connection - @client_nats_connection = NatsClient.new - @client_nats_connection.connect(config.connection_options) + # Disable publisher pending buffer on reconnect + options = config.connection_options.merge(:disable_reconnect_buffer => true) + begin + @client_nats_connection = NatsClient.new + @client_nats_connection.connect(options) + rescue ::Protobuf::Nats::Errors::IOException + @client_nats_connection = nil + raise + end + # Ensure we have a valid connection to the NATS server. @client_nats_connection.flush(5) + + @client_nats_connection.on_disconnect do + logger.warn("Client NATS connection was disconnected") + end + + @client_nats_connection.on_reconnect do + logger.warn("Client NATS connection was reconnected") + end + + @client_nats_connection.on_close do + logger.warn("Client NATS connection was closed") + end @client_nats_connection.on_error do |error| log_error(error) end