From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS15626 46.28.64.0/21 X-Spam-Status: No, score=-2.1 required=3.0 tests=AWL,BAYES_00,RCVD_IN_XBL, URIBL_BLOCKED shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (exit2.tor-proxy.net.ua [46.28.68.158]) by dcvr.yhbt.net (Postfix) with ESMTP id 6EE811F526 for ; Tue, 7 Apr 2015 19:35:32 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] lib/net/protocol.rb (rbuf_fill): avoid exception with read_nonblock Date: Tue, 7 Apr 2015 19:35:30 +0000 Message-Id: <1428435330-21225-1-git-send-email-e@80x24.org> List-Id: Exceptions are noisy in debug output and waste allocations. Use "exception: false" introduced in 2.1 to return symbols for common errors instead. Follow-up commits will be prepared to reduce EOFError exceptions to further quiet debug output and IO.select may be replaced by io/wait methods if available to reduce allocations. --- lib/net/protocol.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 2547701..5a20c5d 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -149,23 +149,21 @@ module Net # :nodoc: BUFSIZE = 1024 * 16 def rbuf_fill - begin - @rbuf << @io.read_nonblock(BUFSIZE) - rescue IO::WaitReadable - if IO.select([@io], nil, nil, @read_timeout) - retry - else - raise Net::ReadTimeout - end - rescue IO::WaitWritable + case rv = @io.read_nonblock(BUFSIZE, exception: false) + when String + return @rbuf << rv + when :wait_readable + IO.select([@io], nil, nil, @read_timeout) or raise Net::ReadTimeout + # continue looping + when :wait_writable # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable. # http://www.openssl.org/support/faq.html#PROG10 - if IO.select(nil, [@io], nil, @read_timeout) - retry - else - raise Net::ReadTimeout - end - end + IO.select(nil, [@io], nil, @read_timeout) or raise Net::ReadTimeout + # continue looping + when nil + # callers do not care about backtrace, so avoid allocating for it + raise EOFError, 'end of file reached', [] + end while true end def rbuf_consume(len) -- EW