From: Tanaka Akira Date: 2011-07-30T00:29:45+09:00 Subject: [ruby-core:38598] Re: [Ruby 1.9 - Feature #5101][Open] allow optional timeout for TCPSocket.new 2011/7/29 Eric Wong : > Like my original example in the ticket? > > addr = Socket.pack_sockaddr_in(9418, "127.0.0.1") > s = Socket.new(:AF_INET, :SOCK_STREAM, 0) > begin > s.connect_nonblock(addr) > rescue Errno::EINPROGRESS > IO.select(nil, [s], nil, 0.5) or raise Timeout::Error > end > > I can only seem to shorten it to the following, is there a better way? > I can't find it looking through ext/socket/... > > addr = Addrinfo.tcp("127.0.0.1", 9418) > s = Socket.new(:AF_INET, :SOCK_STREAM) > begin > s.connect_nonblock(addr) > rescue Errno::EINPROGRESS > IO.select(nil, [s], nil, 0.5) or raise Timeout::Error > end I expected such code in client socket library because socket library doesn't provide timeout feature (and Timeout exception class). > Anyways I would like to be able to implement open_timeout in net/http > without using threads (with timeout.rb). Also see I tested net/http using Socket class as follows. This doesn't cause any problems with test-all. Index: lib/net/http.rb =================================================================== --- lib/net/http.rb (revision 32734) +++ lib/net/http.rb (working copy) @@ -763,7 +763,7 @@ def connect D "opening connection to #{conn_address()}..." - s = timeout(@open_timeout) { TCPSocket.open(conn_address(), conn_port()) } + s = timeout(@open_timeout) { Socket.tcp(conn_address(), conn_port()) } D "opened" if use_ssl? ssl_parameters = Hash.new I remember multiple IP addresses issue. The behavior of timeout is not clear if two or more IP addresses are given for a hostname. -- Tanaka Akira