yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [RFC] wbuf: store busy signal inside this object
Date: Fri, 20 Mar 2015 02:52:09 +0000	[thread overview]
Message-ID: <20150320025209.GA5295@dcvr.yhbt.net> (raw)

This should make it easier to track state for asynchronous
proxy_pass buffering.
---
 RFC because of the emphasis on "should"...

 lib/yahns/http_response.rb |  4 ++--
 lib/yahns/wbuf.rb          | 12 +++++++-----
 test/test_wbuf.rb          |  7 ++++---
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index 0b0296f..1ef2bbf 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -67,7 +67,7 @@ module Yahns::HttpResponse # :nodoc:
       alive = Yahns::StreamFile.new(body, alive, offset, count)
       body = nil
     end
-    wbuf = Yahns::Wbuf.new(body, alive, self.class.output_buffer_tmpdir)
+    wbuf = Yahns::Wbuf.new(body, alive, self.class.output_buffer_tmpdir, ret)
     rv = wbuf.wbuf_write(self, header)
     body.each { |chunk| rv = wbuf.wbuf_write(self, chunk) } if body
     wbuf_maybe(wbuf, rv)
@@ -199,7 +199,7 @@ module Yahns::HttpResponse # :nodoc:
           chunk = rv # hope the skb grows when we loop into the trywrite
         when :wait_writable, :wait_readable
           if k.output_buffering
-            wbuf = Yahns::Wbuf.new(body, alive, k.output_buffer_tmpdir)
+            wbuf = Yahns::Wbuf.new(body, alive, k.output_buffer_tmpdir, rv)
             rv = wbuf.wbuf_write(self, chunk)
             break
           else
diff --git a/lib/yahns/wbuf.rb b/lib/yahns/wbuf.rb
index 4eed2c5..21dbb08 100644
--- a/lib/yahns/wbuf.rb
+++ b/lib/yahns/wbuf.rb
@@ -29,14 +29,15 @@ require_relative 'wbuf_common'
 # to be a scalability issue.
 class Yahns::Wbuf # :nodoc:
   include Yahns::WbufCommon
+  attr_reader :busy
 
-  def initialize(body, persist, tmpdir)
+  def initialize(body, persist, tmpdir, busy)
     @tmpio = nil
     @tmpdir = tmpdir
     @sf_offset = @sf_count = 0
     @wbuf_persist = persist # whether or not we keep the connection alive
     @body = body
-    @bypass = false
+    @busy = busy
   end
 
   def wbuf_write(client, buf)
@@ -47,8 +48,8 @@ class Yahns::Wbuf # :nodoc:
     when nil
       return # yay! hopefully we don't have to buffer again
     when :wait_writable, :wait_readable
-      @bypass = false # ugh, continue to buffering to file
-    end while @bypass
+      @busy = rv
+    end until @busy
 
     @tmpio ||= Yahns::TmpIO.new(@tmpdir)
     @sf_count += @tmpio.write(buf)
@@ -57,6 +58,7 @@ class Yahns::Wbuf # :nodoc:
       @sf_count -= rv
       @sf_offset += rv
     when :wait_writable, :wait_readable
+      @busy = rv
       return rv
     else
       raise "BUG: #{rv.nil ? "EOF" : rv.inspect} on tmpio " \
@@ -67,7 +69,7 @@ class Yahns::Wbuf # :nodoc:
     # to disk if we can help it.
     @tmpio = @tmpio.close
     @sf_offset = 0
-    @bypass = true
+    @busy = false
     nil
   end
 
diff --git a/test/test_wbuf.rb b/test/test_wbuf.rb
index bfdfa2b..e6f8790 100644
--- a/test/test_wbuf.rb
+++ b/test/test_wbuf.rb
@@ -19,7 +19,8 @@ class TestWbuf < Testcase
     buf = "*" * (16384 * 2)
     nr = 1000
     [ true, false ].each do |persist|
-      wbuf = Yahns::Wbuf.new([], persist, Dir.tmpdir)
+      wbuf = Yahns::Wbuf.new([], persist, Dir.tmpdir, :wait_writable)
+      assert_equal :wait_writable, wbuf.busy
       a, b = socketpair
       assert_nil wbuf.wbuf_write(a, "HIHI")
       assert_equal "HIHI", b.read(4)
@@ -69,7 +70,7 @@ class TestWbuf < Testcase
         break
       end while true
     end
-    wbuf = Yahns::Wbuf.new([], true, Dir.tmpdir)
+    wbuf = Yahns::Wbuf.new([], true, Dir.tmpdir, :wait_writable)
     assert_equal :wait_writable, wbuf.wbuf_write(a, buf)
     assert_equal :wait_writable, wbuf.wbuf_flush(a)
 
@@ -93,7 +94,7 @@ class TestWbuf < Testcase
   def test_wbuf_flush_close
     pipe = cloexec_pipe
     persist = true
-    wbuf = Yahns::Wbuf.new(pipe[0], persist, Dir.tmpdir)
+    wbuf = Yahns::Wbuf.new(pipe[0], persist, Dir.tmpdir, :wait_writable)
     refute wbuf.respond_to?(:close) # we don't want this for HttpResponse body
     sp = socketpair
     rv = nil
-- 
EW

                 reply	other threads:[~2015-03-20  2:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/yahns/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150320025209.GA5295@dcvr.yhbt.net \
    --to=e@80x24.org \
    --cc=yahns-public@yhbt.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/yahns.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).