Skip to content

Commit

Permalink
Abstract some of Connection async logic into Request.
Browse files Browse the repository at this point in the history
  • Loading branch information
macournoyer committed Mar 17, 2009
1 parent 1d26f87 commit 8be9327
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 3 additions & 7 deletions lib/thin/connection.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/thin/request.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8be9327

Please sign in to comment.