diff --git a/lib/goliath/request.rb b/lib/goliath/request.rb index 816aaed0..2e7606d0 100644 --- a/lib/goliath/request.rb +++ b/lib/goliath/request.rb @@ -214,8 +214,9 @@ def post_process(results) # Writes each chunk of the response data in a new tick. This achieves # streaming, because EventMachine flushes the sent data to the socket at - # the end of each tick. + # the end of each tick. Stops sending data if connection has been closed. def stream_data(chunks, &block) + return if @response.closed? @conn.send_data(chunks.next) EM.next_tick { stream_data(chunks, &block) } rescue StopIteration diff --git a/lib/goliath/response.rb b/lib/goliath/response.rb index 37b0e8cb..69b6ca1b 100644 --- a/lib/goliath/response.rb +++ b/lib/goliath/response.rb @@ -28,6 +28,7 @@ class Response def initialize @headers = Goliath::Headers.new @status = 200 + @closed = false end # Creates the header line for the response @@ -72,6 +73,14 @@ def headers=(key_value_pairs) # @return [Nil] def close body.close if body.respond_to?(:close) + @closed = true + end + + # Returns whether the response has been closed + # + # @return [Boolean] + def closed? + @closed end # Yields each portion of the response