Module: Legion::Transport::Connection

Defined in:
lib/legion/transport/connection.rb

Class Method Summary collapse

Class Method Details

.channelObject

rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/transport/connection.rb', line 57

def channel # rubocop:disable Metrics/AbcSize
  return @channel_thread.value if !@channel_thread.value.nil? && @channel_thread.value.open?

  @channel_thread.value = session.create_channel
  if Legion::Transport::TYPE == 'march_hare'
    @channel_thread.value.basic_qos(Legion::Settings[:transport][:prefetch])
  else
    @channel_thread.value.prefetch(Legion::Settings[:transport][:prefetch])
  end
  @channel_thread.value
end

.channel_open?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/legion/transport/connection.rb', line 78

def channel_open?
  channel.open?
end

.channel_threadObject



74
75
76
# File 'lib/legion/transport/connection.rb', line 74

def channel_thread
  channel
end

.connectorObject



7
8
9
# File 'lib/legion/transport/connection.rb', line 7

def connector
  Legion::Transport::CONNECTOR
end

.sessionObject



69
70
71
72
# File 'lib/legion/transport/connection.rb', line 69

def session
  nil if @session.nil?
  @session.value
end

.session_open?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/legion/transport/connection.rb', line 82

def session_open?
  session.open?
end

.setupObject

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/transport/connection.rb', line 11

def setup # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  Legion::Logging.info("Using transport connector: #{Legion::Transport::CONNECTOR}")

  if @session.respond_to?(:value) && session.respond_to?(:closed?) && session.closed?
    @channel_thread = Concurrent::ThreadLocalVar.new(nil)
  elsif @session.respond_to?(:value) && session.respond_to?(:closed?) && session.open?
    nil
  elsif Legion::Transport::TYPE == 'march_hare'
    @session ||= Concurrent::Atom.new(
      MarchHare.connect(host: Legion::Settings[:transport][:connection][:host],
                        vhost: Legion::Settings[:transport][:connection][:vhost],
                        user: Legion::Settings[:transport][:connection][:user],
                        password: Legion::Settings[:transport][:connection][:password],
                        port: Legion::Settings[:transport][:connection][:port])
    )
    @channel_thread = Concurrent::ThreadLocalVar.new(nil)
    session.start
    session.create_channel.basic_qos(1)
  else
    @session ||= Concurrent::Atom.new(
      connector.new(
        Legion::Settings[:transport][:connection],
        logger: Legion::Logging::Logger.new(level: 'warn'),
        log_level: :info
      )
    )
    @channel_thread = Concurrent::ThreadLocalVar.new(nil)
    session.start
    session.create_channel.basic_qos(20, true)
  end

  if session.respond_to? :on_blocked
    session.on_blocked { Legion::Logging.warn('Legion::Transport is being blocked by RabbitMQ!') }
  end

  if session.respond_to? :on_unblocked
    session.on_unblocked { Legion::Logging.info('Legion::Transport is no longer being blocked by RabbitMQ') }
  end

  if session.respond_to? :after_recovery_completed
    session.after_recovery_completed { Legion::Logging.info('Legion::Transport has completed recovery') }
  end

  true
end

.shutdownObject



86
87
88
# File 'lib/legion/transport/connection.rb', line 86

def shutdown
  session.close
end