Project

General

Profile

Feature #5101

allow optional timeout for TCPSocket.new

Added by normalperson (Eric Wong) about 9 years ago. Updated about 9 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:38538]

Description

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...


Files

Also available in: Atom PDF