lib/protobuf/nats.rb in protobuf-nats-0.1.2 vs lib/protobuf/nats.rb in protobuf-nats-0.1.3
- old
+ new
@@ -20,10 +20,12 @@
module Messages
ACK = "\1".freeze
end
+ GET_CONNECTED_MUTEX = ::Mutex.new
+
def self.config
@config ||= begin
config = ::Protobuf::Nats::Config.new
config.load_from_yml
config
@@ -38,19 +40,22 @@
service_method_name = service_method.to_s.underscore
"rpc.#{service_class_name}.#{service_method_name}"
end
def self.start_client_nats_connection
- @client_nats_connection = ::NATS::IO::Client.new
- @client_nats_connection.connect(config.connection_options)
+ @start_client_nats_connection ||= begin
+ GET_CONNECTED_MUTEX.synchronize do
+ return if @start_client_nats_connection
- # Ensure we have a valid connection to the NATS server.
- @client_nats_connection.flush(5)
+ @client_nats_connection = ::NATS::IO::Client.new
+ @client_nats_connection.connect(config.connection_options)
- true
- end
+ # Ensure we have a valid connection to the NATS server.
+ @client_nats_connection.flush(5)
- def self.ensure_client_nats_connection_was_started
- @ensure_client_nats_connection_was_started ||= start_client_nats_connection
+ true
+ end
+ end
end
+
end
end