about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-19 10:09:57 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-19 17:04:27 -0700
commit53b04c96d38bc6bb5fb3b4874fbf59aae81eb6f0 (patch)
treebf28e1cdefd32bcbe00fb892cade452e278f8f17
parent1e6d3d19da2b62bfe7f8fd7827dcad3ee3fe9923 (diff)
downloadrainbows-53b04c96d38bc6bb5fb3b4874fbf59aae81eb6f0.tar.gz
This makes it easier to write proxies for slow clients that
benefit from keep-alive.  We also need to be careful about
non-HTTP/1.1 connections that can't do keepalive, now.
-rw-r--r--lib/rainbows/ev_core.rb1
-rw-r--r--lib/rainbows/event_machine.rb34
-rw-r--r--lib/rainbows/rev/client.rb15
-rw-r--r--lib/rainbows/rev/deferred_response.rb2
-rwxr-xr-xt/t0030-fast-pipe-response.sh35
5 files changed, 53 insertions, 34 deletions
diff --git a/lib/rainbows/ev_core.rb b/lib/rainbows/ev_core.rb
index dbcdeba..3e64ff9 100644
--- a/lib/rainbows/ev_core.rb
+++ b/lib/rainbows/ev_core.rb
@@ -42,7 +42,6 @@ module Rainbows
         rv = !!(headers['Transfer-Encoding'] =~ %r{\Achunked\z}i)
         rv = false if headers.delete('X-Rainbows-Autochunk') == 'no'
       end
-      headers[CONNECTION] = CLOSE # TODO: allow keep-alive
       write(response_header(status, headers))
       rv
     end
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index 86cb4eb..d6d41a0 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -95,16 +95,14 @@ module Rainbows
         end while true
       end
 
-      # used for streaming sockets and pipes
-      def stream_response(status, headers, io)
-        do_chunk = stream_response_headers(status, headers) if headers
-        mod = do_chunk ? ResponseChunkPipe : ResponsePipe
-        EM.watch(io, mod, self).notify_readable = true
-      end
-
       def em_write_response(response, alive = false)
         status, headers, body = response
-        headers = @hp.headers? ? HH.new(headers) : nil if headers
+        if @hp.headers?
+          headers = HH.new(headers)
+          headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
+        else
+          headers = nil
+        end
         @body = body
 
         if body.respond_to?(:errback) && body.respond_to?(:callback)
@@ -121,23 +119,19 @@ module Rainbows
           st = io.stat
 
           if st.file?
-            if headers
-              headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
-              write(response_header(status, headers))
-            end
+            write(response_header(status, headers)) if headers
             stream = stream_file_data(body.to_path)
             stream.callback { quit } unless alive
             return
           elsif st.socket? || st.pipe?
-            return stream_response(status, headers, io)
+            chunk = stream_response_headers(status, headers) if headers
+            m = chunk ? ResponseChunkPipe : ResponsePipe
+            return EM.watch(io, m, self, alive).notify_readable = true
           end
           # char or block device... WTF? fall through to body.each
         end
 
-        if headers
-          headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
-          write(response_header(status, headers))
-        end
+        write(response_header(status, headers)) if headers
         write_body_each(self, body)
         quit unless alive
       end
@@ -154,8 +148,8 @@ module Rainbows
       # so a single buffer for all clients will work safely
       BUF = ''
 
-      def initialize(client)
-        @client = client
+      def initialize(client, alive)
+        @client, @alive = client, alive
       end
 
       def notify_readable
@@ -172,8 +166,8 @@ module Rainbows
       end
 
       def unbind
+        @client.quit unless @alive
         @io.close
-        @client.quit
       end
     end
 
diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb
index f067d1b..5c61109 100644
--- a/lib/rainbows/rev/client.rb
+++ b/lib/rainbows/rev/client.rb
@@ -52,6 +52,10 @@ module Rainbows
         schedule_write
       end
 
+      def next
+        @deferred_bodies.shift
+      end
+
       def timeout?
         @_write_buffer.empty? && @deferred_bodies.empty? and close.nil?
       end
@@ -69,25 +73,20 @@ module Rainbows
         status, headers, body = response
         headers = @hp.headers? ? HH.new(headers) : nil
 
+        headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE if headers
         if body.respond_to?(:to_path)
           io = body_to_io(body)
           st = io.stat
 
           if st.file?
-            if headers
-              headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
-              write(response_header(status, headers))
-            end
+            write(response_header(status, headers)) if headers
             return defer_body(to_sendfile(io))
           elsif st.socket? || st.pipe?
             return stream_response(status, headers, io, body)
           end
           # char or block device... WTF? fall through to body.each
         end
-        if headers
-          headers[CONNECTION] = alive ? KEEP_ALIVE : CLOSE
-          write(response_header(status, headers))
-        end
+        write(response_header(status, headers)) if headers
         write_body_each(self, body)
       end
 
diff --git a/lib/rainbows/rev/deferred_response.rb b/lib/rainbows/rev/deferred_response.rb
index de348bb..cc4ea10 100644
--- a/lib/rainbows/rev/deferred_response.rb
+++ b/lib/rainbows/rev/deferred_response.rb
@@ -20,7 +20,7 @@ module Rainbows
 
       def on_close
         @do_chunk and @client.write("0\r\n\r\n")
-        @client.quit
+        @client.next
         @body.respond_to?(:close) and @body.close
       end
     end # class DeferredResponse
diff --git a/t/t0030-fast-pipe-response.sh b/t/t0030-fast-pipe-response.sh
index 5acbc01..f81029a 100755
--- a/t/t0030-fast-pipe-response.sh
+++ b/t/t0030-fast-pipe-response.sh
@@ -2,10 +2,10 @@
 . ./test-lib.sh
 test -r random_blob || die "random_blob required, run with 'make $0'"
 
-t_plan 6 "fast pipe response for $model"
+t_plan 10 "fast pipe response for $model"
 
 t_begin "setup and startup" && {
-        rtmpfiles err
+        rtmpfiles err out
         rainbows_setup $model
         rainbows -E none -D fast-pipe-response.ru -c $unicorn_config
         rainbows_wait_start
@@ -13,6 +13,7 @@ t_begin "setup and startup" && {
 
 t_begin "read random blob sha1" && {
         random_blob_sha1=$(rsha1 < random_blob)
+        three_sha1=$(cat random_blob random_blob random_blob | rsha1)
 }
 
 t_begin "single request matches" && {
@@ -25,12 +26,38 @@ t_begin "Content-Length header preserved in response" && {
         grep "^< Content-Length:" $err
 }
 
+t_begin "send three keep-alive requests" && {
+        sha1=$(curl -vsSf 2> $err \
+               http://$listen/ http://$listen/ http://$listen/ | rsha1)
+        test -n "$sha1"
+        test x"$sha1" = x"$three_sha1"
+}
+
+t_begin "ensure responses were all keep-alive" && {
+        test 3 -eq $(grep '< Connection: keep-alive' < $err | wc -l)
+}
+
+t_begin "HTTP/1.0 test" && {
+        sha1=$(curl -0 -v 2> $err -sSf http://$listen/ | rsha1)
+        test $sha1 = $random_blob_sha1
+        grep '< Connection: close' < $err
+}
+
+t_begin "HTTP/0.9 test" && {
+        (
+                printf 'GET /\r\n'
+                rsha1 < $fifo > $tmp &
+                wait
+                echo ok > $ok
+        ) | socat - TCP:$listen > $fifo
+        test $(cat $tmp) = $random_blob_sha1
+        test xok = x$(cat $ok)
+}
+
 t_begin "shutdown server" && {
         kill -QUIT $rainbows_pid
 }
 
-dbgcat r_err
-
 t_begin "check stderr" && check_stderr
 
 t_done