From: Akira Tanaka <akr@...>
Date: 2011-08-10T22:21:59+09:00
Subject: [ruby-core:38899] [Ruby 1.9 - Feature #5101][Closed] allow optional timeout for TCPSocket.new


Issue #5101 has been updated by Akira Tanaka.

Status changed from Assigned to Closed


----------------------------------------
Feature #5101: allow optional timeout for TCPSocket.new
http://redmine.ruby-lang.org/issues/5101

Author: Eric Wong
Status: Closed
Priority: Low
Assignee: Akira Tanaka
Category: ext
Target version: 1.9.x


Proposed API would be:

   TCPSocket.new(remote_host, remote_port,
		 connect_timeout: 0.5, local_host: nil, local_port: nil)

Or:

   TCPSocket.new(remote_host, remote_port, connect_timeout: 0.5)

For the common case.


This would only timeout for establishing the TCP connection, not DNS
resolution.  DNS resolution can be covered by Feature #5100.

The existing form for the (rarely-used) local_host/local_port args will
still be supported for backwards compatibility:

   TCPSocket.new(remote_host, remote_port, local_host=nil, local_port=nil)


The current construct for doing a non-blocking connect with timeout
is too verbose:

    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

And could be replaced with:

    TCPSocket.new("127.0.0.1", 9418, connect_timeout: 0.5)

I am not sure what exception TCPSocket.new should return.  Timeout::Error
seems like a reasonable choice...



-- 
http://redmine.ruby-lang.org