yahns Ruby server user/dev discussion
 help / color / Atom feed
* [PATCH] use IO#pread if available in Ruby 2.5
@ 2018-07-16 11:46 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2018-07-16 11:46 UTC (permalink / raw)
  To: yahns-public

In the future, this will allow sharing open files across
different clients when serving static files.  For now, it
saves us one syscall.
---
 lib/yahns/http_client.rb    | 19 +++++++++++++++----
 lib/yahns/openssl_client.rb |  5 +----
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/lib/yahns/http_client.rb b/lib/yahns/http_client.rb
index fd97624..d55a0db 100644
--- a/lib/yahns/http_client.rb
+++ b/lib/yahns/http_client.rb
@@ -322,12 +322,23 @@ def app_hijacked?(env, res)
     true
   end
 
-  def trysendio(io, offset, count)
-    return 0 if count == 0
+  def do_pread(io, count, offset)
     count = 0x4000 if count > 0x4000
     buf = Thread.current[:yahns_sfbuf] ||= ''.dup
-    io.pos = offset
-    str = io.read(count, buf) or return # nil for EOF
+    if io.respond_to?(:pread)
+      io.pread(count, offset, buf)
+    else
+      io.pos = offset
+      io.read(count, buf)
+    end
+  rescue EOFError
+    warn "BUG: do_pread overreach:\n #{caller.join("\n ")}\n"
+    nil
+  end
+
+  def trysendio(io, offset, count)
+    return 0 if count == 0
+    str = do_pread(io, count, offset) or return # nil for EOF
     n = 0
     case rv = kgio_trywrite(str)
     when String # partial write, keep trying
diff --git a/lib/yahns/openssl_client.rb b/lib/yahns/openssl_client.rb
index c090083..d3caacb 100644
--- a/lib/yahns/openssl_client.rb
+++ b/lib/yahns/openssl_client.rb
@@ -93,10 +93,7 @@ def trysendio(io, offset, count)
 
     case buf = @ssl_blocked
     when nil
-      count = 0x4000 if count > 0x4000
-      buf = Thread.current[:yahns_sfbuf] ||= ''.dup
-      io.pos = offset
-      buf = io.read(count, buf) or return # nil for EOF
+      buf = do_pread(io, count, offset) or return # nil for EOF
       buf = @ssl_blocked = buf.dup
     when Exception
       raise buf
-- 
EW


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-16 11:46 [PATCH] use IO#pread if available in Ruby 2.5 Eric Wong

yahns Ruby server user/dev discussion

Archives are clonable:
	git clone --mirror https://yhbt.net/yahns-public
	git clone --mirror http://ou63pmih66umazou.onion/yahns-public

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.lang.ruby.yahns
	nntp://ou63pmih66umazou.onion/inbox.comp.lang.ruby.yahns

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox