Skip to content

HTTPS clone URL

Subversion checkout URL

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