From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 0033A1F597 for ; Mon, 16 Jul 2018 11:46:21 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Subject: [PATCH] use IO#pread if available in Ruby 2.5 Date: Mon, 16 Jul 2018 11:46:20 +0000 Message-Id: <20180716114620.6207-1-e@80x24.org> List-Id: 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