Class: Lagomorph::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/lagomorph/session.rb

Constant Summary collapse

CONNECTION_PARAM_KEYS =
[
  :host,
  :heartbeat_interval,
  :user,
  :username,
  :password,
  :port
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_params) ⇒ Session

Returns a new instance of Session.



19
20
21
22
23
24
# File 'lib/lagomorph/session.rb', line 19

def initialize(connection_params)
  @connection_params = connection_params.select { |key,_|
    CONNECTION_PARAM_KEYS.include?(key)
  }
  @mutex = Monitor.new
end

Class Method Details

.connect(connection_params) ⇒ Object



14
15
16
# File 'lib/lagomorph/session.rb', line 14

def self.connect(connection_params)
  new(connection_params).tap(&:open_connection)
end

Instance Method Details

#close_connectionObject



36
37
38
39
40
41
# File 'lib/lagomorph/session.rb', line 36

def close_connection
  return if @connection.nil? || @connection.closed?
  @mutex.synchronize do
    @connection.close
  end
end

#create_channel(prefetch = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lagomorph/session.rb', line 43

def create_channel(prefetch = nil)
  @mutex.synchronize do
    channel = @connection.create_channel
    if Lagomorph.using_bunny?
      channel.prefetch(prefetch)
    else
      channel.prefetch = prefetch
    end
    channel
  end
end

#open_connectionObject



26
27
28
29
30
31
32
33
34
# File 'lib/lagomorph/session.rb', line 26

def open_connection
  @mutex.synchronize do
    @connection ||= if Lagomorph.using_bunny?
                      ::Bunny.new(@connection_params).tap(&:start)
                    else
                      ::MarchHare.connect(@connection_params)
                    end
  end
end