Class: MultipleMan::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/multiple_man/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel) ⇒ Connection

Returns a new instance of Connection.



78
79
80
81
# 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

Instance Attribute Details

#topicObject

Returns the value of attribute topic.



75
76
77
# File 'lib/multiple_man/connection.rb', line 75

def topic
  @topic
end

Class Method Details

.channelObject



28
29
30
31
32
33
34
35
36
37
# 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_gcObject



59
60
61
62
63
# 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

.connectObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

.connectionObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

.reset!Object



65
66
67
68
69
70
71
72
73
# 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

Instance Method Details

#topic_nameObject



83
84
85
# File 'lib/multiple_man/connection.rb', line 83

def topic_name
  MultipleMan.configuration.topic_name
end