class RailwayIpc::ConnectionManager

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

Public Class Methods

new() click to toggle source
# File lib/railway_ipc/connection_manager.rb, line 12
def initialize
  establish_connection
end

Public Instance Methods

channel() click to toggle source
# File lib/railway_ipc/connection_manager.rb, line 31
def channel
  return @channel if connected?

  establish_connection
  @channel
end
connected?() click to toggle source
# File lib/railway_ipc/connection_manager.rb, line 38
def connected?
  @connection&.connected? && @channel&.open?
end
establish_connection() click to toggle source
# 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

Private Instance Methods

amqp_url() click to toggle source
# File lib/railway_ipc/connection_manager.rb, line 44
def amqp_url
  @amqp_url ||= ENV.fetch('RAILWAY_RABBITMQ_CONNECTION_URL', 'amqp://guest:guest@localhost:5672')
end
settings() click to toggle source
# File lib/railway_ipc/connection_manager.rb, line 48
def settings
  @settings ||= AMQ::Settings.parse_amqp_url(amqp_url)
end