diff options
author | Eric Wong <e@80x24.org> | 2016-08-31 02:39:11 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2016-08-31 02:48:17 +0000 |
commit | cea8d77382e42132f4a433d59a5dc37fcc295ec3 (patch) | |
tree | 213938a6b3623c748011494c5d2550be09528fc3 | |
parent | 2280594d422cbaa90362105afe695629115ebf8b (diff) | |
download | mogilefs-client-cea8d77382e42132f4a433d59a5dc37fcc295ec3.tar.gz |
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.
-rw-r--r-- | lib/mogilefs/socket/pure_ruby.rb | 20 |
1 files 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 @@ require 'io/wait' 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) |