Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 130 lines (107 sloc) 2.928 kB
1173bab @rtomayko require yaml where it's needed
rtomayko authored
1 require 'yaml'
44de948 @mojombo works on the first try!
authored
2 require 'eventmachine'
8bfa196 @mojombo use Logger for timestamped log messages and add connection accept log…
authored
3 require 'logger'
7d14e3a @cmelbye Require socket to get the required Socket class.
cmelbye authored
4 require 'socket'
44de948 @mojombo works on the first try!
authored
5
6 require 'proxymachine/client_connection'
7 require 'proxymachine/server_connection'
8
8bfa196 @mojombo use Logger for timestamped log messages and add connection accept log…
authored
9 LOGGER = Logger.new(STDOUT)
10
161e9cf @cmelbye Changing back to a class-based namespace
cmelbye authored
11 class ProxyMachine
ae0564d @mojombo Release 1.2.4
authored
12 VERSION = '1.2.4'
13
920d39f @mojombo add fast shutdown for TERM and INT signals
authored
14 MAX_FAST_SHUTDOWN_SECONDS = 10
15
1908a62 @mojombo set procline to show name and number of connections
authored
16 def self.update_procline
d82ad48 @mojombo add max conn count to procline
authored
17 $0 = "proxymachine #{VERSION} - #{@@name} #{@@listen} - #{self.stats} cur/max/tot conns"
18 end
19
20 def self.stats
21 "#{@@counter}/#{@@maxcounter}/#{@@totalcounter}"
1908a62 @mojombo set procline to show name and number of connections
authored
22 end
23
fa57c0b @mojombo trap TERM signal and gracefully exit
authored
24 def self.count
25 @@counter
118e6d5 @mojombo not so verbose
authored
26 end
27
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
28 def self.incr
534bfd0 @mojombo add totalcount to procline
authored
29 @@totalcounter += 1
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
30 @@counter += 1
d82ad48 @mojombo add max conn count to procline
authored
31 @@maxcounter = @@counter if @@counter > @@maxcounter
1908a62 @mojombo set procline to show name and number of connections
authored
32 self.update_procline
33 @@counter
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
34 end
35
36 def self.decr
37 @@counter -= 1
fa57c0b @mojombo trap TERM signal and gracefully exit
authored
38 if $server.nil?
8bfa196 @mojombo use Logger for timestamped log messages and add connection accept log…
authored
39 LOGGER.info "Waiting for #{@@counter} connections to finish."
fa57c0b @mojombo trap TERM signal and gracefully exit
authored
40 end
1908a62 @mojombo set procline to show name and number of connections
authored
41 self.update_procline
fa57c0b @mojombo trap TERM signal and gracefully exit
authored
42 EM.stop if $server.nil? and @@counter == 0
43 @@counter
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
44 end
45
9fcb3c1 @mojombo simple straight proxy config working
authored
46 def self.set_router(block)
47 @@router = block
48 end
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
49
9fcb3c1 @mojombo simple straight proxy config working
authored
50 def self.router
51 @@router
52 end
ffa57cd @mojombo getting decent throughput with fewer errors now
authored
53
920d39f @mojombo add fast shutdown for TERM and INT signals
authored
54 def self.graceful_shutdown(signal)
55 EM.stop_server($server) if $server
8bfa196 @mojombo use Logger for timestamped log messages and add connection accept log…
authored
56 LOGGER.info "Received #{signal} signal. No longer accepting new connections."
57 LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish."
920d39f @mojombo add fast shutdown for TERM and INT signals
authored
58 $server = nil
59 EM.stop if ProxyMachine.count == 0
60 end
61
62 def self.fast_shutdown(signal)
63 EM.stop_server($server) if $server
8bfa196 @mojombo use Logger for timestamped log messages and add connection accept log…
authored
64 LOGGER.info "Received #{signal} signal. No longer accepting new connections."
65 LOGGER.info "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
66 LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish."
920d39f @mojombo add fast shutdown for TERM and INT signals
authored
67 $server = nil
68 EM.stop if ProxyMachine.count == 0
69 Thread.new do
70 sleep MAX_FAST_SHUTDOWN_SECONDS
71 exit!
72 end
73 end
74
b4d1001 @rtomayko custom connection error callbacks + connect timeouts
rtomayko authored
75 def self.set_connect_error_callback(&block)
76 @@connect_error_callback = block
77 end
78
79 def self.connect_error_callback
80 @@connect_error_callback
81 end
82
678fbae @rtomayko inactivity timeouts and inactivity error callbacks
rtomayko authored
83 def self.set_inactivity_error_callback(&block)
84 @@inactivity_error_callback = block
85 end
86
87 def self.inactivity_error_callback
88 @@inactivity_error_callback
89 end
90
1908a62 @mojombo set procline to show name and number of connections
authored
91 def self.run(name, host, port)
534bfd0 @mojombo add totalcount to procline
authored
92 @@totalcounter = 0
d82ad48 @mojombo add max conn count to procline
authored
93 @@maxcounter = 0
1908a62 @mojombo set procline to show name and number of connections
authored
94 @@counter = 0
95 @@name = name
affd20a @mojombo add host:port to procline
authored
96 @@listen = "#{host}:#{port}"
b4d1001 @rtomayko custom connection error callbacks + connect timeouts
rtomayko authored
97 @@connect_error_callback ||= proc { |remote| }
fae7d5c @rtomayko missed that when resolving rebase conflicts
rtomayko authored
98 @@inactivity_error_callback ||= proc { |remote| }
1908a62 @mojombo set procline to show name and number of connections
authored
99 self.update_procline
28a7277 @mojombo enable kqueue and epoll
authored
100 EM.epoll
101
9fcb3c1 @mojombo simple straight proxy config working
authored
102 EM.run do
03f0aac @cmelbye Moving ClientConnection and ServerConnection to the ProxyMachine module.
cmelbye authored
103 ProxyMachine::ClientConnection.start(host, port)
b9c9e48 @mojombo use QUIT instead of TERM for graceful exit to match up with nginx and…
authored
104 trap('QUIT') do
920d39f @mojombo add fast shutdown for TERM and INT signals
authored
105 self.graceful_shutdown('QUIT')
106 end
107 trap('TERM') do
108 self.fast_shutdown('TERM')
109 end
110 trap('INT') do
111 self.fast_shutdown('INT')
fa57c0b @mojombo trap TERM signal and gracefully exit
authored
112 end
9fcb3c1 @mojombo simple straight proxy config working
authored
113 end
114 end
115 end
116
117 module Kernel
118 def proxy(&block)
119 ProxyMachine.set_router(block)
120 end
b4d1001 @rtomayko custom connection error callbacks + connect timeouts
rtomayko authored
121
122 def proxy_connect_error(&block)
123 ProxyMachine.set_connect_error_callback(&block)
124 end
678fbae @rtomayko inactivity timeouts and inactivity error callbacks
rtomayko authored
125
126 def proxy_inactivity_error(&block)
127 ProxyMachine.set_inactivity_error_callback(&block)
128 end
b4d1001 @rtomayko custom connection error callbacks + connect timeouts
rtomayko authored
129 end
Something went wrong with that request. Please try again.