[前][次][番号順一覧][スレッド一覧]

ruby-changes:38138

From: normal <ko1@a...>
Date: Sat, 11 Apr 2015 06:05:44 +0900 (JST)
Subject: [ruby-changes:38138] normal:r50219 (trunk): lib/net/protocol.rb (rbuf_fill): avoid exception with read_nonblock

normal	2015-04-11 06:05:29 +0900 (Sat, 11 Apr 2015)

  New Revision: 50219

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50219

  Log:
    lib/net/protocol.rb (rbuf_fill): avoid exception with read_nonblock
    
    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.
    
    [ruby-core:68787] [Feature #11044]

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/protocol.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50218)
+++ ChangeLog	(revision 50219)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr 11 04:46:42 2015  Eric Wong  <e@8...>
+
+	* lib/net/protocol.rb (rbuf_fill): avoid exception with read_nonblock
+	  [ruby-core:68787] [Feature #11044]
+
 Fri Apr 10 23:57:44 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* dir.c (need_normalization): use getattrlist() if fgetattrlist()
Index: lib/net/protocol.rb
===================================================================
--- lib/net/protocol.rb	(revision 50218)
+++ lib/net/protocol.rb	(revision 50219)
@@ -149,23 +149,21 @@ module Net # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/protocol.rb#L149
     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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]