about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <yahns-public@yhbt.net>2017-03-06 03:25:20 +0000
committerEric Wong <yahns-public@yhbt.net>2017-03-06 03:25:20 +0000
commitba72f7d658db56e3be38ece1dab8a5329c73e721 (patch)
tree1ce320df949327c24a9de47e757ba400593e681d
parent9af6d47aec508077ad6f752d10cc97d40319c3b3 (diff)
downloadyahns-remove-kgio.tar.gz
Since we're getting rid of kgio, we'll need to remove
writev support as it's not in mainline Ruby.  I've never
checked if writev support was ever worth it for performance,
and benchmarks will be coming.

At least this simplifies req_res.rb quite a bit.
-rw-r--r--lib/yahns/chunk_body.rb15
-rw-r--r--lib/yahns/http_response.rb6
-rw-r--r--lib/yahns/openssl_client.rb4
-rw-r--r--lib/yahns/proxy_http_response.rb16
-rw-r--r--lib/yahns/req_res.rb51
-rw-r--r--lib/yahns/tmpio.rb1
-rw-r--r--lib/yahns/wbuf.rb11
-rw-r--r--test/test_tmpio.rb6
8 files changed, 46 insertions, 64 deletions
diff --git a/lib/yahns/chunk_body.rb b/lib/yahns/chunk_body.rb
index aab803b..7153ad9 100644
--- a/lib/yahns/chunk_body.rb
+++ b/lib/yahns/chunk_body.rb
@@ -1,24 +1,19 @@
 # -*- encoding: binary -*-
 # Copyright (C) 2016 all contributors <yahns-public@yhbt.net>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
-# frozen_string_literal: true
+# frozen_string_literal: false
 
 class Yahns::ChunkBody # :nodoc:
-  def initialize(body, vec)
+  def initialize(body)
     @body = body
-    @vec = vec
   end
 
   def each
-    vec = @vec
-    vec[2] = "\r\n".freeze
     @body.each do |chunk|
-      vec[0] = "#{chunk.bytesize.to_s(16)}\r\n"
-      vec[1] = chunk
-      # vec[2] never changes: "\r\n" above
-      yield vec
+      chunk = "#{chunk.bytesize.to_s(16)}\r\n#{chunk}\r\n"
+      yield chunk
+      chunk.clear
     end
-    vec.clear
     yield "0\r\n\r\n".freeze
   end
 
diff --git a/lib/yahns/http_response.rb b/lib/yahns/http_response.rb
index 86b7f56..7fa70ff 100644
--- a/lib/yahns/http_response.rb
+++ b/lib/yahns/http_response.rb
@@ -167,7 +167,7 @@ module Yahns::HttpResponse # :nodoc:
 
     if !term && chunk_ok && !hdr_only
       term = true
-      body = Yahns::ChunkBody.new(body, opt)
+      body = Yahns::ChunkBody.new(body)
       buf << "Transfer-Encoding: chunked\r\n".freeze
     end
     alive &&= (term || hdr_only)
@@ -205,10 +205,10 @@ module Yahns::HttpResponse # :nodoc:
       if wbuf
         rv = wbuf.wbuf_write(self, x)
       else
-        case rv = String === x ? kgio_trywrite(x) : kgio_trywritev(x)
+        case rv = kgio_trywrite(x)
         when nil # all done, likely and good!
           break
-        when String, Array
+        when String
           x = rv # hope the skb grows when we loop into the trywrite
         when :wait_writable, :wait_readable
           if self.class.output_buffering
diff --git a/lib/yahns/openssl_client.rb b/lib/yahns/openssl_client.rb
index a86d701..cf4941c 100644
--- a/lib/yahns/openssl_client.rb
+++ b/lib/yahns/openssl_client.rb
@@ -51,10 +51,6 @@ module Yahns::OpenSSLClient # :nodoc:
     rv
   end
 
-  def kgio_trywritev(buf)
-    kgio_trywrite(buf.join)
-  end
-
   def kgio_syssend(buf, flags)
     kgio_trywrite(buf)
   end
diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index d9c878e..9f75fec 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -1,7 +1,7 @@
 # -*- encoding: binary -*-
 # Copyright (C) 2015-2016 all contributors <yahns-public@yhbt.net>
 # License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
-# frozen_string_literal: true
+# frozen_string_literal: false
 
 require_relative 'wbuf_lite'
 
@@ -31,9 +31,9 @@ module Yahns::HttpResponse # :nodoc:
   def proxy_write(wbuf, buf, req_res)
     unless wbuf
       # no write buffer, try to write directly to the client socket
-      case rv = String === buf ? kgio_trywrite(buf) : kgio_trywritev(buf)
+      case rv = kgio_trywrite(buf)
       when nil then return # done writing buf, likely
-      when String, Array # partial write, hope the skb grows
+      when String # partial write, hope the skb grows
         buf = rv
       when :wait_writable, :wait_readable
         wbuf = req_res.resbuf ||= wbuf_alloc(req_res)
@@ -157,16 +157,18 @@ module Yahns::HttpResponse # :nodoc:
     when String
       if len
         kcar.body_bytes_left -= tmp.size # progress for body_eof? => true
+        clr = nil
       elsif chunk
         kcar.filter_body(chunk, rbuf = tmp) # progress for body_eof? => true
         next if chunk.empty? # call req_res.read_nonblock for more
-        tmp = chunk_out(chunk)
+        clr = tmp = chunk_out(chunk)
+        chunk.clear
       elsif alive # HTTP/1.0 upstream, HTTP/1.1 client
-        tmp = chunk_out(tmp)
+        clr = tmp = chunk_out(tmp)
       # else # HTTP/1.0 upstream, HTTP/1.0 client, do nothing
       end
       wbuf = proxy_write(wbuf, tmp, req_res)
-      chunk.clear if chunk
+      clr.clear if clr
       if Yahns::WbufLite === wbuf
         req_res.proxy_trailers = [ rbuf.dup, tip ] if chunk && kcar.body_eof?
         return proxy_unbuffer(wbuf)
@@ -296,7 +298,7 @@ module Yahns::HttpResponse # :nodoc:
   # of String#bytesize because all the IO read methods return a binary
   # string when given a maximum read length
   def chunk_out(buf)
-    [ "#{buf.size.to_s(16)}\r\n", buf, "\r\n".freeze ]
+    "#{buf.size.to_s(16)}\r\n#{buf}\r\n"
   end
 
   def trailer_out(tlr)
diff --git a/lib/yahns/req_res.rb b/lib/yahns/req_res.rb
index 0fa4285..958015a 100644
--- a/lib/yahns/req_res.rb
+++ b/lib/yahns/req_res.rb
@@ -1,7 +1,7 @@
 # -*- encoding: binary -*-
 # Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
 # License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
-# frozen_string_literal: true
+# frozen_string_literal: false
 # Only used by Yahns::ProxyPass
 require 'kcar' # gem install kcar
 require 'kgio'
@@ -64,7 +64,7 @@ class Yahns::ReqRes < Kgio::Socket # :nodoc:
 
       end while true # case @resbuf
 
-    when Array # [ (str|vec), rack.input, chunked? ]
+    when Array # [ str, rack.input, chunked? ]
       send_req_body(req) # returns nil or :wait_writable
     when String # buffered request header
       send_req_buf(req)
@@ -80,11 +80,14 @@ class Yahns::ReqRes < Kgio::Socket # :nodoc:
   end
 
   def send_req_body_chunk(buf)
-    case rv = String === buf ? kgio_trywrite(buf) : kgio_trywritev(buf)
-    when String, Array
+    case rv = kgio_trywrite(buf)
+    when nil
+      return rv # done
+    when String
       buf.replace(rv) # retry loop on partial write
-    when :wait_writable, nil
+    when :wait_writable
       # :wait_writable = upstream is reading slowly and making us wait
+      @rrstate[0] = buf # for the next time we're called
       return rv
     else
       abort "BUG: #{rv.inspect} from kgio_trywrite*"
@@ -92,39 +95,33 @@ class Yahns::ReqRes < Kgio::Socket # :nodoc:
   end
 
   # returns :wait_readable if complete, :wait_writable if not
-  def send_req_body(req) # @rrstate == [ (str|vec), rack.input, chunked? ]
+  def send_req_body(req) # @rrstate == [ str, rack.input, chunked? ]
     buf, input, chunked = req
 
     # send the first buffered chunk or vector
     rv = send_req_body_chunk(buf) and return rv # :wait_writable
 
     # yay, sent the first chunk, now read the body!
-    rbuf = buf
     if chunked
-      if String === buf # initial body
-        req[0] = buf = []
-      else
-        # try to reuse the biggest non-frozen buffer we just wrote;
-        rbuf = buf.max_by(&:size)
-        rbuf = ''.dup if rbuf.frozen? # unlikely...
+      # Note: input (env['rack.input']) is fully-buffered by default so
+      # we should not be waiting on a slow network resource when reading
+      # input.  However, some weird configs may disable this on LANs
+      # and we may wait indefinitely on input.read here...
+
+      rbuf = buf
+      while input.read(0x2000, rbuf)
+        buf = "#{rbuf.size.to_s(16)}\r\n#{rbuf}\r\n"
+        rbuf.clear
+        rv = send_req_body_chunk(buf) and return rv # :wait_writable
+        buf.clear
       end
-    end
-
-    # Note: input (env['rack.input']) is fully-buffered by default so
-    # we should not be waiting on a slow network resource when reading
-    # input.  However, some weird configs may disable this on LANs
-    # and we may wait indefinitely on input.read here...
-    while input.read(0x2000, rbuf)
-      if chunked
-        buf[0] = "#{rbuf.size.to_s(16)}\r\n".freeze
-        buf[1] = rbuf
-        buf[2] = "\r\n".freeze
+    else
+      while input.read(0x2000, buf)
+        rv = send_req_body_chunk(buf) and return rv # :wait_writable
       end
-      rv = send_req_body_chunk(buf) and return rv # :wait_writable
+      buf.clear # all done, clear the big buffer
     end
 
-    rbuf.clear # all done, clear the big buffer
-
     # we cannot use respond_to?(:close) here since Rack::Lint::InputWrapper
     # tries to prevent that (and hijack means all Rack specs go out the door)
     case input
diff --git a/lib/yahns/tmpio.rb b/lib/yahns/tmpio.rb
index 9e36d93..c14dc8a 100644
--- a/lib/yahns/tmpio.rb
+++ b/lib/yahns/tmpio.rb
@@ -8,7 +8,6 @@ require 'tmpdir'
 # well with unlinked files.  This one is much shorter, easier
 # to understand, and slightly faster (no delegation).
 class Yahns::TmpIO < File # :nodoc:
-  include Kgio::PipeMethods
 
   # creates and returns a new File object.  The File is unlinked
   # immediately, switched to binary mode, and userspace output
diff --git a/lib/yahns/wbuf.rb b/lib/yahns/wbuf.rb
index 3abc5f9..0eb7267 100644
--- a/lib/yahns/wbuf.rb
+++ b/lib/yahns/wbuf.rb
@@ -40,16 +40,11 @@ class Yahns::Wbuf # :nodoc:
     @busy = false
   end
 
-  def wbuf_writev(buf)
-    @tmpio.kgio_writev(buf)
-    buf.inject(0) { |n, s| n += s.size }
-  end
-
   def wbuf_write(c, buf)
     # try to bypass the VFS layer and write directly to the socket
     # if we're all caught up
-    case rv = String === buf ? c.kgio_trywrite(buf) : c.kgio_trywritev(buf)
-    when String, Array
+    case rv = c.kgio_trywrite(buf)
+    when String
       buf = rv # retry in loop
     when nil
       return # yay! hopefully we don't have to buffer again
@@ -59,7 +54,7 @@ class Yahns::Wbuf # :nodoc:
 
     @tmpio ||= Yahns::TmpIO.new(c.class.output_buffer_tmpdir)
     # n.b.: we rely on O_APPEND in TmpIO, here
-    @sf_count += String === buf ? @tmpio.write(buf) : wbuf_writev(buf)
+    @sf_count += @tmpio.write(buf)
 
     # we spent some time copying to the FS, try to write to
     # the socket again in case some space opened up...
diff --git a/test/test_tmpio.rb b/test/test_tmpio.rb
index 3bcf3ca..3b2ca88 100644
--- a/test/test_tmpio.rb
+++ b/test/test_tmpio.rb
@@ -9,12 +9,10 @@ class TestTmpIO < Testcase
     skip 'sendfile missing' unless IO.instance_methods.include?(:sendfile)
   end
 
-  def test_writev
+  def test_write_and_sendfile
     a, b = UNIXSocket.pair
-    a.extend Kgio::PipeMethods
     tmpio = Yahns::TmpIO.new(Dir.tmpdir)
-    ary = [ "hello\n".freeze, "world\n".freeze ].freeze
-    tmpio.kgio_trywritev(ary)
+    tmpio.write("hello\nworld\n")
     a.trysendfile(tmpio, 0, 12)
     assert_equal "hello\nworld\n", b.read(12)
   ensure