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

VERSION =
'1.2.4'
MAX_FAST_SHUTDOWN_SECONDS =
10

Class Method Summary collapse

Class Method Details

.connect_error_callbackObject



79
80
81
# File 'lib/proxymachine.rb', line 79

def self.connect_error_callback
  @@connect_error_callback
end

.countObject



24
25
26
# File 'lib/proxymachine.rb', line 24

def self.count
  @@counter
end

.decrObject



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

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



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

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



54
55
56
57
58
59
60
# File 'lib/proxymachine.rb', line 54

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



87
88
89
# File 'lib/proxymachine.rb', line 87

def self.inactivity_error_callback
  @@inactivity_error_callback
end

.incrObject



28
29
30
31
32
33
34
# File 'lib/proxymachine.rb', line 28

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

.routerObject



50
51
52
# File 'lib/proxymachine.rb', line 50

def self.router
  @@router
end

.run(name, host, port) ⇒ Object



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

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



75
76
77
# File 'lib/proxymachine.rb', line 75

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

.set_inactivity_error_callback(&block) ⇒ Object



83
84
85
# File 'lib/proxymachine.rb', line 83

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

.set_router(block) ⇒ Object



46
47
48
# File 'lib/proxymachine.rb', line 46

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

.statsObject



20
21
22
# File 'lib/proxymachine.rb', line 20

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

.update_proclineObject



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

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