Project

General

Profile

Actions

Feature #5101

closed

allow optional timeout for TCPSocket.new

Added by normalperson (Eric Wong) over 13 years ago. Updated over 13 years ago.

Status:
Closed
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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0