From 5dea67ecf3dd868e7b375312cdef2c4651b11437 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 16 Dec 2009 23:57:08 -0800 Subject: cleanup: consolidate write_nonblock error handling --- lib/rainbows/base.rb | 7 +------ lib/rainbows/error.rb | 9 +++++++++ lib/rainbows/fiber/base.rb | 2 +- lib/rainbows/revactor.rb | 13 +------------ 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/lib/rainbows/base.rb b/lib/rainbows/base.rb index a735b82..9bbe049 100644 --- a/lib/rainbows/base.rb +++ b/lib/rainbows/base.rb @@ -10,11 +10,6 @@ module Rainbows include Rainbows::Const G = Rainbows::G - def handle_error(client, e) - msg = Error.response(e) and client.write_nonblock(msg) - rescue - end - def init_worker_process(worker) super(worker) G.tmp = worker.tmp @@ -65,7 +60,7 @@ module Rainbows # if the socket is already closed or broken. We'll always ensure # the socket is closed at the end of this function rescue => e - handle_error(client, e) + Error.write(client, e) ensure client.close end diff --git a/lib/rainbows/error.rb b/lib/rainbows/error.rb index 2d8cd94..1e25626 100644 --- a/lib/rainbows/error.rb +++ b/lib/rainbows/error.rb @@ -4,6 +4,15 @@ module Rainbows class Error class << self + # if we get any error, try to write something back to the client + # assuming we haven't closed the socket, but don't get hung up + # if the socket is already closed or broken. We'll always ensure + # the socket is closed at the end of this function + def write(io, e) + msg = Error.response(e) and io.write_nonblock(msg) + rescue + end + def app(e) G.server.logger.error "app error: #{e.inspect}" G.server.logger.error e.backtrace.join("\n") diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb index 995b4af..1617c54 100644 --- a/lib/rainbows/fiber/base.rb +++ b/lib/rainbows/fiber/base.rb @@ -99,7 +99,7 @@ module Rainbows HttpResponse.write(client, response, out) end while alive and hp.reset.nil? and env.clear rescue => e - handle_error(io, e) + Error.write(io, e) ensure G.cur -= 1 RD.delete(client) diff --git a/lib/rainbows/revactor.rb b/lib/rainbows/revactor.rb index ff0e429..b410bda 100644 --- a/lib/rainbows/revactor.rb +++ b/lib/rainbows/revactor.rb @@ -68,7 +68,7 @@ module Rainbows end while alive and hp.reset.nil? and env.clear rescue ::Revactor::TCP::ReadError rescue => e - handle_error(client, e) + Error.write(client.instance_eval { @_io }, e) ensure client.close end @@ -121,17 +121,6 @@ module Rainbows rescue Errno::EMFILE => e end - # if we get any error, try to write something back to the client - # assuming we haven't closed the socket, but don't get hung up - # if the socket is already closed or broken. We'll always ensure - # the socket is closed at the end of this function - def handle_error(client, e) - # this is Revactor implementation dependent - msg = Error.response(e) and - client.instance_eval { @_io.write_nonblock(msg) } - rescue - end - def revactorize_listeners LISTENERS.map do |s| case s -- cgit v1.2.3-24-ge0c7