Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 81 lines (69 sloc) 1.754 kb
44de9487 »
2009-04-24 works on the first try!
1 require 'rubygems'
2 require 'eventmachine'
3
4 require 'proxymachine/client_connection'
5 require 'proxymachine/server_connection'
6
9fcb3c19 »
2009-04-25 simple straight proxy config working
7 class ProxyMachine
920d39f8 »
2009-10-01 add fast shutdown for TERM and INT signals
8 MAX_FAST_SHUTDOWN_SECONDS = 10
9
fa57c0b7 »
2009-10-01 trap TERM signal and gracefully exit
10 def self.count
11 @@counter ||= 0
12 @@counter
118e6d5a »
2009-04-25 not so verbose
13 end
14
ffa57cd9 »
2009-04-25 getting decent throughput with fewer errors now
15 def self.incr
16 @@counter ||= 0
17 @@counter += 1
18 end
19
20 def self.decr
21 @@counter ||= 0
22 @@counter -= 1
fa57c0b7 »
2009-10-01 trap TERM signal and gracefully exit
23 if $server.nil?
24 puts "Waiting for #{@@counter} connections to finish."
25 end
26 EM.stop if $server.nil? and @@counter == 0
27 @@counter
ffa57cd9 »
2009-04-25 getting decent throughput with fewer errors now
28 end
29
9fcb3c19 »
2009-04-25 simple straight proxy config working
30 def self.set_router(block)
31 @@router = block
32 end
ffa57cd9 »
2009-04-25 getting decent throughput with fewer errors now
33
9fcb3c19 »
2009-04-25 simple straight proxy config working
34 def self.router
35 @@router
36 end
ffa57cd9 »
2009-04-25 getting decent throughput with fewer errors now
37
920d39f8 »
2009-10-01 add fast shutdown for TERM and INT signals
38 def self.graceful_shutdown(signal)
39 EM.stop_server($server) if $server
40 puts "Received #{signal} signal. No longer accepting new connections."
41 puts "Waiting for #{ProxyMachine.count} connections to finish."
42 $server = nil
43 EM.stop if ProxyMachine.count == 0
44 end
45
46 def self.fast_shutdown(signal)
47 EM.stop_server($server) if $server
48 puts "Received #{signal} signal. No longer accepting new connections."
49 puts "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
50 puts "Waiting for #{ProxyMachine.count} connections to finish."
51 $server = nil
52 EM.stop if ProxyMachine.count == 0
53 Thread.new do
54 sleep MAX_FAST_SHUTDOWN_SECONDS
55 exit!
56 end
57 end
58
51767553 »
2009-04-25 pass port and host command line args through
59 def self.run(host, port)
28a72773 »
2009-04-27 enable kqueue and epoll
60 EM.epoll
61
9fcb3c19 »
2009-04-25 simple straight proxy config working
62 EM.run do
51767553 »
2009-04-25 pass port and host command line args through
63 EventMachine::Protocols::ClientConnection.start(host, port)
b9c9e488 »
2009-10-01 use QUIT instead of TERM for graceful exit to match up with nginx and…
64 trap('QUIT') do
920d39f8 »
2009-10-01 add fast shutdown for TERM and INT signals
65 self.graceful_shutdown('QUIT')
66 end
67 trap('TERM') do
68 self.fast_shutdown('TERM')
69 end
70 trap('INT') do
71 self.fast_shutdown('INT')
fa57c0b7 »
2009-10-01 trap TERM signal and gracefully exit
72 end
9fcb3c19 »
2009-04-25 simple straight proxy config working
73 end
74 end
75 end
76
77 module Kernel
78 def proxy(&block)
79 ProxyMachine.set_router(block)
80 end
44de9487 »
2009-04-24 works on the first try!
81 end
Something went wrong with that request. Please try again.