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
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_params) ⇒ Session

Returns a new instance of Session.



21
22
23
24
25
26
# File 'lib/lagomorph/session.rb', line 21

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/lagomorph/session.rb', line 4

def connection
  @connection
end

Class Method Details

.connect(connection_params) ⇒ Object



16
17
18
# File 'lib/lagomorph/session.rb', line 16

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

Instance Method Details

#close_connectionObject



38
39
40
41
42
43
# File 'lib/lagomorph/session.rb', line 38

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

#create_channel(prefetch = nil) ⇒ Object



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

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



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

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