Sha256: 83a92681fb846e7e76f303692d9e96bae1a94311220cf967827d762e743e7eaf

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

require 'thread'

module ActivePublisher
  module Connection
    CONNECTION_MUTEX = ::Mutex.new
    NETWORK_RECOVERY_INTERVAL = 1.freeze

    def self.connected?
      connection.try(:connected?)
    end

    def self.connection
      CONNECTION_MUTEX.synchronize do
        return @connection if @connection
        @connection = create_connection
      end
    end

    def self.disconnect!
      CONNECTION_MUTEX.synchronize do
        if @connection && @connection.connected?
          @connection.close
        end

        @connection = nil
      end
    rescue Timeout::Error
      # No-op ... this happens sometimes on MRI disconnect
    end

    # Private API
    def self.create_connection
      if ::RUBY_PLATFORM == "java"
        connection = ::MarchHare.connect(connection_options)
      else
        connection = ::Bunny.new(connection_options)
        connection.start
        connection
      end
    end
    private_class_method :create_connection

    def self.connection_options
      {
        :heartbeat                     => ::ActivePublisher.configuration.heartbeat,
        :hosts                         => ::ActivePublisher.configuration.hosts,
        :pass                          => ::ActivePublisher.configuration.password,
        :port                          => ::ActivePublisher.configuration.port,
        :user                          => ::ActivePublisher.configuration.username,
        :continuation_timeout          => ::ActivePublisher.configuration.timeout * 1_000.0, #convert sec to ms
        :automatically_recover         => true,
        :network_recovery_interval     => NETWORK_RECOVERY_INTERVAL,
        :recover_from_connection_close => true,
      }
    end
    private_class_method :connection_options
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_publisher-0.2.1-java lib/active_publisher/connection.rb
active_publisher-0.2.1 lib/active_publisher/connection.rb
active_publisher-0.2.1.pre.pre1-java lib/active_publisher/connection.rb
active_publisher-0.2.1.pre.pre1 lib/active_publisher/connection.rb
active_publisher-0.2.0.pre-java lib/active_publisher/connection.rb
active_publisher-0.2.0.pre lib/active_publisher/connection.rb