Skip to content

Commit

Permalink
use Logger for timestamped log messages and add connection accept log…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
mojombo committed Oct 14, 2009
1 parent 8dabd2a commit 8bfa196
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
6 changes: 3 additions & 3 deletions bin/proxymachine
Expand Up @@ -38,8 +38,8 @@ rescue Exception => e
if e.instance_of?(SystemExit)
raise
else
puts 'Uncaught exception'
puts e.message
puts e.backtrace.join("\n")
LOGGER.info 'Uncaught exception'
LOGGER.info e.message
LOGGER.info e.backtrace.join("\n")
end
end
2 changes: 1 addition & 1 deletion examples/git.rb
Expand Up @@ -6,7 +6,7 @@ class GitRouter
# Look at the routing table and return the correct address for +name+
# Returns "<host>:<port>" e.g. "ae8f31c.example.com:9418"
def self.lookup(name)
puts "Proxying for user #{name}"
LOGGER.info "Proxying for user #{name}"
"localhost:9418"
end
end
Expand Down
15 changes: 9 additions & 6 deletions lib/proxymachine.rb
@@ -1,9 +1,12 @@
require 'rubygems'
require 'eventmachine'
require 'logger'

require 'proxymachine/client_connection'
require 'proxymachine/server_connection'

LOGGER = Logger.new(STDOUT)

class ProxyMachine
MAX_FAST_SHUTDOWN_SECONDS = 10

Expand All @@ -30,7 +33,7 @@ def self.incr
def self.decr
@@counter -= 1
if $server.nil?
puts "Waiting for #{@@counter} connections to finish."
LOGGER.info "Waiting for #{@@counter} connections to finish."
end
self.update_procline
EM.stop if $server.nil? and @@counter == 0
Expand All @@ -47,17 +50,17 @@ def self.router

def self.graceful_shutdown(signal)
EM.stop_server($server) if $server
puts "Received #{signal} signal. No longer accepting new connections."
puts "Waiting for #{ProxyMachine.count} connections to finish."
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

def self.fast_shutdown(signal)
EM.stop_server($server) if $server
puts "Received #{signal} signal. No longer accepting new connections."
puts "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds."
puts "Waiting for #{ProxyMachine.count} connections to finish."
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
Expand Down
17 changes: 9 additions & 8 deletions lib/proxymachine/client_connection.rb
Expand Up @@ -3,12 +3,13 @@ module Protocols
class ClientConnection < Connection
def self.start(host, port)
$server = EM.start_server(host, port, self)
puts "Listening on #{host}:#{port}"
puts "Send QUIT to quit after waiting for all connections to finish."
puts "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish."
LOGGER.info "Listening on #{host}:#{port}"
LOGGER.info "Send QUIT to quit after waiting for all connections to finish."
LOGGER.info "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish."
end

def post_init
LOGGER.info "Accepted #{peer}"
@buffer = []
@tries = 0
ProxyMachine.incr
Expand All @@ -29,14 +30,14 @@ def receive_data(data)
end
rescue => e
close_connection
puts "#{e.class} - #{e.message}"
LOGGER.info "#{e.class} - #{e.message}"
end

def ensure_server_side_connection
@timer.cancel if @timer
unless @server_side
commands = ProxyMachine.router.call(@buffer.join)
puts "#{peer} #{commands.inspect}"
LOGGER.info "#{peer} #{commands.inspect}"
close_connection unless commands.instance_of?(Hash)
if remote = commands[:remote]
m, host, port = *remote.match(/^(.+):(.+)$/)
Expand Down Expand Up @@ -64,18 +65,18 @@ def ensure_server_side_connection
def try_server_connect(host, port)
@server_side = ServerConnection.request(host, port, self)
proxy_incoming_to(@server_side, 10240)
puts "Successful connection to #{host}:#{port}."
LOGGER.info "Successful connection to #{host}:#{port}."
true
rescue => e
if @tries < 10
@tries += 1
puts "Failed on server connect attempt #{@tries}. Trying again..."
LOGGER.info "Failed on server connect attempt #{@tries}. Trying again..."
@timer.cancel if @timer
@timer = EventMachine::Timer.new(0.1) do
self.ensure_server_side_connection
end
else
puts "Failed after ten connection attempts."
LOGGER.info "Failed after ten connection attempts."
end
false
end
Expand Down

0 comments on commit 8bfa196

Please sign in to comment.