Class: ProxyMachine

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

Defined Under Namespace

Classes: ClientConnection, ServerConnection

Constant Summary collapse

MAX_FAST_SHUTDOWN_SECONDS =
10
VERSION =
self.version

Class Method Summary collapse

Class Method Details

.connect_error_callbackObject



77
78
79
# File 'lib/proxymachine.rb', line 77

def self.connect_error_callback
  @@connect_error_callback
end

.countObject



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

def self.count
  @@counter
end

.decrObject



34
35
36
37
38
39
40
41
42
# File 'lib/proxymachine.rb', line 34

def self.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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/proxymachine.rb', line 60

def self.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 #{ProxyMachine.count} connections to finish."
  $server = nil
  EM.stop if ProxyMachine.count == 0
  Thread.new do
    sleep MAX_FAST_SHUTDOWN_SECONDS
    exit!
  end
end

.graceful_shutdown(signal) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/proxymachine.rb', line 52

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

.inactivity_error_callbackObject



85
86
87
# File 'lib/proxymachine.rb', line 85

def self.inactivity_error_callback
  @@inactivity_error_callback
end

.incrObject



26
27
28
29
30
31
32
# File 'lib/proxymachine.rb', line 26

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

.routerObject



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

def self.router
  @@router
end

.run(name, host, port) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/proxymachine.rb', line 89

def self.run(name, host, port)
  @@totalcounter = 0
  @@maxcounter = 0
  @@counter = 0
  @@name = name
  @@listen = "#{host}:#{port}"
  @@connect_error_callback ||= proc { |remote| }
  @@inactivity_error_callback ||= proc { |remote| }
  self.update_procline
  EM.epoll

  EM.run do
    ProxyMachine::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_connect_error_callback(&block) ⇒ Object



73
74
75
# File 'lib/proxymachine.rb', line 73

def self.set_connect_error_callback(&block)
  @@connect_error_callback = block
end

.set_inactivity_error_callback(&block) ⇒ Object



81
82
83
# File 'lib/proxymachine.rb', line 81

def self.set_inactivity_error_callback(&block)
  @@inactivity_error_callback = block
end

.set_router(block) ⇒ Object



44
45
46
# File 'lib/proxymachine.rb', line 44

def self.set_router(block)
  @@router = block
end

.statsObject



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

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

.update_proclineObject



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

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

.versionObject



114
115
116
117
118
119
# File 'lib/proxymachine.rb', line 114

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