From 3495d59763e6159975debf32728dc53fc41c5ea1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 27 Dec 2010 20:25:39 -0800 Subject: several response body#close fixes Some middlewares require the Rack env to be preserved all the way through to close, so we'll ensure all request models preserve it. We also need to better response body wrappers/proxies always get fired properly when returning. IO.copy_stream and "sendfile" gem users could hit cases where wrappers did not fire properly. --- lib/rainbows/sync_close.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/rainbows/sync_close.rb (limited to 'lib/rainbows/sync_close.rb') diff --git a/lib/rainbows/sync_close.rb b/lib/rainbows/sync_close.rb new file mode 100644 index 0000000..a336262 --- /dev/null +++ b/lib/rainbows/sync_close.rb @@ -0,0 +1,37 @@ +# -*- encoding: binary -*- +# :enddoc: +require 'thread' +class Rainbows::SyncClose + def initialize(body) + @body = body + @mutex = Mutex.new + @cv = ConditionVariable.new + @mutex.synchronize do + yield self + @cv.wait(@mutex) + end + end + + def respond_to?(m) + @body.respond_to?(m) + end + + def to_path + @body.to_path + end + + def each(&block) + @body.each(&block) + end + + def to_io + @body.to_io + end + + # called by the writer thread to wake up the original thread (in #initialize) + def close + @body.close + ensure + @mutex.synchronize { @cv.signal } + end +end -- cgit v1.2.3-24-ge0c7