Tanaka Akira <akr / fsij.org> wrote:
> 2011/7/30 Tanaka Akira <akr / fsij.org>:
> 
> > I expected such code in client socket library because
> > socket library doesn't provide timeout feature (and Timeout exception class).
> 
> On second thought, Errno::ETIMEDOUT can be a candidate.

OK; sounds good.

> > I remember multiple IP addresses issue.
> > The behavior of timeout is not clear if two or more IP addresses are
> > given for a hostname.
> 
> This is still a problem.

I only want this timeout to affect connect() time, DNS timeout can
be separate (Feature #5100).  I don't know what to do about multiple
IP addresses, it may be better on a case-by-case basis or until total
time runs out.

Maybe even have an option to do a non-blocking connect() to all IPs
simultaneously and use the first one that returns writability
with IO.select:

  # This may make some server/network admins unhappy and even be
  # considered abusive and get clients banned, but it could be useful
  # in some situations; too
	sockets = addresses.map do |addr|
    s = Socket.new(:AF_INET, :SOCK_STREAM)
    begin
      s.connect_nonblock(addr)
    rescue Errno::EINPROGRESS
    end
    s
  end
  ready = IO.select(nil, sockets, nil, 0.5) or raise Errno::ETIMEDOUT
  keep = nil
  ready[1].each do |sock|
    if keep
      sock.close
    else
      begin
        do_something(sock)
        keep = sock
      rescue
        sock.close
      end
    end
  end
  do_more_things(keep)

-- 
Eric Wong