Class: RailwayIpc::ConnectionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/railway_ipc/connection_manager.rb

Overview

RabbitMQ connection manager. Ensures there is a single RabbitMQ connection and channel per thread, which prevents channel leaks.

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



12
13
14
# File 'lib/railway_ipc/connection_manager.rb', line 12

def initialize
  establish_connection
end

Instance Method Details

#channelObject



31
32
33
34
35
36
# File 'lib/railway_ipc/connection_manager.rb', line 31

def channel
  return @channel if connected?

  establish_connection
  @channel
end

#connected?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/railway_ipc/connection_manager.rb', line 38

def connected?
  @connection&.connected? && @channel&.open?
end

#establish_connectionObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/railway_ipc/connection_manager.rb', line 16

def establish_connection
  @connection = Bunny.new(
    host: settings[:host],
    user: settings[:user],
    pass: settings[:pass],
    port: settings[:port],
    vhost: settings[:vhost] || '/',
    logger: RailwayIpc.logger
  )
  @connection.start
  @channel = @connection.create_channel

  @connection
end