class MultipleMan::Connection

Attributes

channel[RW]
topic[RW]

Public Class Methods

channel() click to toggle source
# File lib/multiple_man/connection.rb, line 28
def self.channel
  Thread.current.thread_variable_get(:multiple_man_current_channel) || begin
    channel = connection.create_channel
    channel_gc.push(channel)
    channel.confirm_select if MultipleMan.configuration.publisher_confirms
    Thread.current.thread_variable_set(:multiple_man_current_channel, channel)

    channel
  end
end
channel_gc() click to toggle source
# File lib/multiple_man/connection.rb, line 59
def self.channel_gc
  @channel_gc ||= ChannelMaintenance::GC.new(
    MultipleMan.configuration,
    ChannelMaintenance::Reaper.new(MultipleMan.configuration))
end
connect() { |new(channel)| ... } click to toggle source
# File lib/multiple_man/connection.rb, line 8
def self.connect
  yield new(channel)
  Thread.current[:multiple_man_exception_retry_count] = 0
rescue Bunny::Exception, Timeout::Error => e
  recovery_options = MultipleMan.configuration.connection_recovery
  MultipleMan.logger.debug "Bunny Error: #{e.inspect}"

  retry_count = Thread.current[:multiple_man_exception_retry_count] || 0
  retry_count += 1

  if retry_count < recovery_options[:max_retries]
    Thread.current[:multiple_man_exception_retry_count] = retry_count
    sleep recovery_options[:time_between_retries]
    retry
  else
    Thread.current[:multiple_man_exception_retry_count] = 0
    raise ConnectionError, e
  end
end
connection() click to toggle source
# File lib/multiple_man/connection.rb, line 39
def self.connection
  @mutex.synchronize do
    @connection ||= begin
      connection = Bunny.new(
        MultipleMan.configuration.connection,
        {
          heartbeat_interval: 5,
          automatically_recover: true,
          recover_from_connection_close: true,
          network_recovery_interval: MultipleMan.configuration.connection_recovery[:time_before_reconnect]
        }.merge(MultipleMan.configuration.bunny_opts)
      )
      MultipleMan.logger.debug "Connecting to #{MultipleMan.configuration.connection}"
      connection.start

      connection
    end
  end
end
new(channel) click to toggle source
# File lib/multiple_man/connection.rb, line 78
def initialize(channel)
  self.channel = channel
  self.topic = channel.topic(topic_name, MultipleMan.configuration.exchange_opts)
end
reset!() click to toggle source
# File lib/multiple_man/connection.rb, line 65
def self.reset!
  @mutex.synchronize do
    @connection.close if @connection
    @connection = nil

    @channel_gc.stop if @channel_gc
    @channel_gc = nil
  end
end

Public Instance Methods

topic_name() click to toggle source
# File lib/multiple_man/connection.rb, line 83
def topic_name
  MultipleMan.configuration.topic_name
end