Tanaka Akira <akr / fsij.org> wrote:
> 2011/7/27 Eric Wong <normalperson / yhbt.net>:
> >
> > 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)
> 
> How about Socket class?
> You can implement timeout on Socket.
> 
> I think you need full power of socket API,
> so you should use low level bindings for socket API i.e. Socket class.
> 
> Note that Socket is not so cumbersome since Ruby 1.9.2.

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

Anyways I would like to be able to implement open_timeout in net/http
without using threads (with timeout.rb).  Also see
http://redmine.ruby-lang.org/issues/5100 for resolv.rb timeouts.

-- 
Eric Wong