Browse files

Abstract some of Connection async logic into Request.

  • Loading branch information...
1 parent 1d26f87 commit 8be9327a49919196ddaaf7cc605e38b271edeeb7 @macournoyer macournoyer committed Mar 17, 2009
Showing with 15 additions and 8 deletions.
  1. +3 −7 lib/thin/connection.rb
  2. +12 −1 lib/thin/request.rb
View
10 lib/thin/connection.rb
@@ -62,14 +62,10 @@ def pre_process
# Add client info to the request env
@request.remote_address = remote_address
- # TODO - remove excess documentation / move it somewhere more sensible.
- # (interface specs!) - (rack)
-
# Connection may be closed unless the App#call response was a [-1, ...]
# It should be noted that connection objects will linger until this
# callback is no longer referenced, so be tidy!
- @request.env['async.callback'] = method(:post_process)
- @request.env['async.close'] = EM::DefaultDeferrable.new
+ @request.async_callback = method(:post_process)
# When we're under a non-async framework like rails, we can still spawn
# off async responses using the callback info, so there's little point
@@ -130,7 +126,7 @@ def handle_error
end
def close_request_response
- @request.env['async.close'].succeed
+ @request.async_close.succeed
@request.close rescue nil
@response.close rescue nil
end
@@ -154,7 +150,7 @@ def terminate_request
# Called when the connection is unbinded from the socket
# and can no longer be used to process requests.
def unbind
- @request.env['async.close'].succeed if @request.env['async.close']
+ @request.async_close.succeed if @request.async_close
@response.body.fail if @response.body.respond_to?(:fail)
@backend.connection_finished(self)
end
View
13 lib/thin/request.rb
@@ -26,14 +26,16 @@ class Request
CONNECTION = 'HTTP_CONNECTION'.freeze
KEEP_ALIVE_REGEXP = /\bkeep-alive\b/i.freeze
CLOSE_REGEXP = /\bclose\b/i.freeze
-
+
# Freeze some Rack header names
RACK_INPUT = 'rack.input'.freeze
RACK_VERSION = 'rack.version'.freeze
RACK_ERRORS = 'rack.errors'.freeze
RACK_MULTITHREAD = 'rack.multithread'.freeze
RACK_MULTIPROCESS = 'rack.multiprocess'.freeze
RACK_RUN_ONCE = 'rack.run_once'.freeze
+ ASYNC_CALLBACK = 'async.callback'.freeze
+ ASYNC_CLOSE = 'async.close'.freeze
# CGI-like request environment variables
attr_reader :env
@@ -128,6 +130,15 @@ def forwarded_for
def threaded=(value)
@env[RACK_MULTITHREAD] = value
end
+
+ def async_callback=(callback)
+ @env[ASYNC_CALLBACK] = callback
+ @env[ASYNC_CLOSE] = EventMachine::DefaultDeferrable.new
+ end
+
+ def async_close
+ @async_close ||= @env[ASYNC_CLOSE]
+ end
# Close any resource used by the request
def close

0 comments on commit 8be9327

Please sign in to comment.