From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) 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.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 857AA203EC for ; Wed, 31 Aug 2016 02:50:47 +0000 (UTC) From: Eric Wong To: mogilefs-client-public@bogomips.org Subject: [PATCH 3/6] socket/pure_ruby: connect with "exception:false" on Ruby 2.3+ Date: Wed, 31 Aug 2016 02:50:43 +0000 Message-Id: <20160831025046.24153-4-e@80x24.org> In-Reply-To: <20160831025046.24153-1-e@80x24.org> References: <20160831025046.24153-1-e@80x24.org> List-Id: Exceptions are expensive for common errors, so avoid raising them under Ruby 2.3+ which allows connect_nonblock to be called with "exception: false" kwarg. --- lib/mogilefs/socket/pure_ruby.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/mogilefs/socket/pure_ruby.rb b/lib/mogilefs/socket/pure_ruby.rb index d57a7c0..59e43e1 100644 --- a/lib/mogilefs/socket/pure_ruby.rb +++ b/lib/mogilefs/socket/pure_ruby.rb @@ -5,13 +5,21 @@ class MogileFS::Socket < Socket include MogileFS::SocketCommon - def self.start(host, port) - sock = new(Socket::AF_INET, Socket::SOCK_STREAM, 0) - begin - sock.connect_nonblock(sockaddr_in(port, host)) - rescue Errno::EINPROGRESS + if RUBY_VERSION.to_f >= 2.3 + def self.start(host, port) + sock = new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + sock.connect_nonblock(sockaddr_in(port, host), :exception => false) + sock.post_init(host, port) + end + else + def self.start(host, port) + sock = new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + begin + sock.connect_nonblock(sockaddr_in(port, host)) + rescue Errno::EINPROGRESS + end + sock.post_init(host, port) end - sock.post_init(host, port) end def self.tcp(host, port, timeout = 5) -- EW