Class: Goliath::Connection
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Goliath::Connection
- Defined in:
- lib/goliath/connection.rb
Constant Summary
- AsyncResponse =
[-1, {}, []].freeze
Instance Attribute Summary (collapse)
-
- (Object) app
Returns the value of attribute app.
-
- (Object) config
Returns the value of attribute config.
-
- (Object) logger
Returns the value of attribute logger.
-
- (Object) options
Returns the value of attribute options.
-
- (Object) request
Returns the value of attribute request.
-
- (Object) response
Returns the value of attribute response.
-
- (Object) status
Returns the value of attribute status.
Instance Method Summary (collapse)
- - (Object) async_process(results)
- - (Boolean) async_response?(results)
- - (Object) close_request_response
- - (Object) log_request(type, response)
- - (Object) post_init
- - (Object) post_process(results)
- - (Object) process
- - (Object) receive_data(data)
- - (Object) remote_address
- - (Object) send_response
- - (Object) stream_close
- - (Object) stream_send(data)
- - (Object) stream_start(status, headers)
- - (Object) terminate_request
- - (Object) unbind
Instance Attribute Details
- (Object) app
Returns the value of attribute app
6 7 8 |
# File 'lib/goliath/connection.rb', line 6 def app @app end |
- (Object) config
Returns the value of attribute config
7 8 9 |
# File 'lib/goliath/connection.rb', line 7 def config @config end |
- (Object) logger
Returns the value of attribute logger
7 8 9 |
# File 'lib/goliath/connection.rb', line 7 def logger @logger end |
- (Object) options
Returns the value of attribute options
7 8 9 |
# File 'lib/goliath/connection.rb', line 7 def @options end |
- (Object) request
Returns the value of attribute request
6 7 8 |
# File 'lib/goliath/connection.rb', line 6 def request @request end |
- (Object) response
Returns the value of attribute response
6 7 8 |
# File 'lib/goliath/connection.rb', line 6 def response @response end |
- (Object) status
Returns the value of attribute status
7 8 9 |
# File 'lib/goliath/connection.rb', line 7 def status @status end |
Instance Method Details
- (Object) async_process(results)
37 38 39 40 41 42 43 |
# File 'lib/goliath/connection.rb', line 37 def async_process(results) @response.status, @response.headers, @response.body = *results log_request(:async, @response) send_response terminate_request end |
- (Boolean) async_response?(results)
83 84 85 |
# File 'lib/goliath/connection.rb', line 83 def async_response?(results) results && results.first == AsyncResponse.first end |
- (Object) close_request_response
92 93 94 95 |
# File 'lib/goliath/connection.rb', line 92 def close_request_response @request.async_close.succeed @response.close rescue nil end |
- (Object) log_request(type, response)
58 59 60 61 62 |
# File 'lib/goliath/connection.rb', line 58 def log_request(type, response) logger.info("#{type} status: #{@response.status}, " + "Content-Length: #{@response.headers['Content-Length']}, " + "Response Time: #{"%.2f" % ((Time.now.to_f - request.env[:start_time]) * 1000)}ms") end |
- (Object) post_init
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/goliath/connection.rb', line 11 def post_init @request = Goliath::Request.new @response = Goliath::Response.new @request.remote_address = remote_address @request.async_callback = method(:async_process) @request.stream_start = method(:stream_start) @request.stream_send = method(:stream_send) @request.stream_close = method(:stream_close) end |
- (Object) post_process(results)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/goliath/connection.rb', line 64 def post_process(results) results = results.to_a return if async_response?(results) @response.status, @response.headers, @response.body = *results log_request(:sync, @response) send_response rescue Exception => e logger.error("#{e.}\n#{e.backtrace.join("\n")}") ensure terminate_request if not async_response?(results) end |
- (Object) process
28 29 30 31 32 33 34 35 |
# File 'lib/goliath/connection.rb', line 28 def process response.send_close = request.env[Goliath::Request::HEADERS]['Connection'] post_process(@app.call(@request.env)) rescue Exception => e logger.error("#{e.}\n#{e.backtrace.join("\n")}") post_process([500, {}, 'An error happened']) end |
- (Object) receive_data(data)
23 24 25 26 |
# File 'lib/goliath/connection.rb', line 23 def receive_data(data) request.parse(data) process if request.finished? end |
- (Object) remote_address
102 103 104 105 106 |
# File 'lib/goliath/connection.rb', line 102 def remote_address Socket.unpack_sockaddr_in(get_peername)[1] rescue Exception nil end |
- (Object) send_response
79 80 81 |
# File 'lib/goliath/connection.rb', line 79 def send_response @response.each { |chunk| send_data(chunk) } end |
- (Object) stream_close
54 55 56 |
# File 'lib/goliath/connection.rb', line 54 def stream_close terminate_request end |
- (Object) stream_send(data)
50 51 52 |
# File 'lib/goliath/connection.rb', line 50 def stream_send(data) send_data(data) end |
- (Object) stream_start(status, headers)
45 46 47 48 |
# File 'lib/goliath/connection.rb', line 45 def stream_start(status, headers) send_data(@response.head) send_data(@response.headers_output) end |
- (Object) terminate_request
87 88 89 90 |
# File 'lib/goliath/connection.rb', line 87 def terminate_request close_connection_after_writing rescue nil close_request_response end |
- (Object) unbind
97 98 99 100 |
# File 'lib/goliath/connection.rb', line 97 def unbind @request.async_close.succeed unless @request.async_close.nil? @response.body.fail if @response.body.respond_to?(:fail) end |