Class: RabbitCage

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitcage.rb,
lib/rabbitcage/filter.rb,
lib/rabbitcage/client_connection.rb,
lib/rabbitcage/server_connection.rb

Defined Under Namespace

Classes: ClientConnection, Filter, ServerConnection

Constant Summary collapse

MAX_FAST_SHUTDOWN_SECONDS =
10
VERSION =
self.version

Class Method Summary collapse

Class Method Details

.build_filterObject



63
64
65
66
# File 'lib/rabbitcage.rb', line 63

def build_filter
  RabbitCage.config.call
  @@filter = Filter.build
end

.configObject



52
53
54
# File 'lib/rabbitcage.rb', line 52

def config
  @@config
end

.countObject



26
27
28
# File 'lib/rabbitcage.rb', line 26

def count
  @@counter
end

.decrObject



38
39
40
41
42
43
44
45
46
# File 'lib/rabbitcage.rb', line 38

def decr
  @@counter -= 1
  if $server.nil?
    LOGGER.info "Waiting for #{@@counter} connections to finish."
  end
  self.update_procline
  EM.stop if $server.nil? and @@counter == 0
  @@counter
end

.fast_shutdown(signal) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rabbitcage.rb', line 80

def fast_shutdown(signal)
  EM.stop_server($server) if $server
  LOGGER.info "Received #{signal} signal. No longer accepting new connections."
  LOGGER.info "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
  LOGGER.info "Waiting for #{RabbitCage.count} connections to finish."
  $server = nil
  EM.stop if RabbitCage.count == 0
  Thread.new do
    sleep MAX_FAST_SHUTDOWN_SECONDS
    exit!
  end
end

.filter(frame) ⇒ Object



68
69
70
# File 'lib/rabbitcage.rb', line 68

def filter frame
  Filter.filter frame
end

.graceful_shutdown(signal) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rabbitcage.rb', line 72

def graceful_shutdown(signal)
  EM.stop_server($server) if $server
  LOGGER.info "Received #{signal} signal. No longer accepting new connections."
  LOGGER.info "Waiting for #{RabbitCage.count} connections to finish."
  $server = nil
  EM.stop if RabbitCage.count == 0
end

.incrObject



30
31
32
33
34
35
36
# File 'lib/rabbitcage.rb', line 30

def incr
  @@totalcounter += 1
  @@counter += 1
  @@maxcounter = @@counter if @@counter > @@maxcounter
  self.update_procline
  @@counter
end

.rabbit_hostObject



56
57
58
# File 'lib/rabbitcage.rb', line 56

def rabbit_host
  @@remote_host
end

.rabbit_portObject



60
61
62
# File 'lib/rabbitcage.rb', line 60

def rabbit_port
  @@remote_port
end

.run(name, host, port, rhost, rport, log_level) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rabbitcage.rb', line 93

def run(name, host, port, rhost, rport, log_level)
  @@totalcounter = 0
  @@maxcounter = 0
  @@counter = 0
  @@name = name
  @@listen = "#{host}:#{port}"
  @@remote_host = rhost
  @@remote_port = rport
  LOGGER.level = log_level

  self.update_procline
  EM.epoll
  
  RabbitCage.build_filter
  
  EM.run do
    RabbitCage::ClientConnection.start(host, port)
    trap('QUIT') do
      self.graceful_shutdown('QUIT')
    end
    trap('TERM') do
      self.fast_shutdown('TERM')
    end
    trap('INT') do
      self.fast_shutdown('INT')
    end
  end
end

.set_config(block) ⇒ Object



48
49
50
# File 'lib/rabbitcage.rb', line 48

def set_config(block)
  @@config = block
end

.statsObject



22
23
24
# File 'lib/rabbitcage.rb', line 22

def stats
  "#{@@counter}/#{@@maxcounter}/#{@@totalcounter}"
end

.update_proclineObject



18
19
20
# File 'lib/rabbitcage.rb', line 18

def update_procline
  $0 = "rabbitcage #{VERSION} - #{@@name} #{@@listen} - #{self.stats} cur/max/tot conns"
end

.versionObject



122
123
124
125
126
127
# File 'lib/rabbitcage.rb', line 122

def version
  yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml])))
  "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}"
rescue
  'unknown'
end