xiewenwei / gmail.com wrote:
> net/http, net/pop, net/smtp and net/ftp use `Timeout.timeout` to calculate connect_timeout.
> Timeout.timeout is slow. It creates and destroys a thread every time.
> Timeout.timeout is also dangerous. see [Timeout: Ruby's Most Dangerous API](http://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/)

I agree with eliminating Timeout, but I don't think your
solution is enough because it does not cover timeouts for
DNS resolution (getaddrinfo(3) calls).

For timeouts, we would need to use resolv.rb instead of getaddrinfo(3)
provided by libc to do timeouts without a separate thread.  I started
adding timeouts to resolv.rb last year but can't remember how far I got...
I'm not sure if resolv.rb supports all the features of a modern
getaddrinfo(3), either, AFAIK, not many people use resolv.rb.

> It is more effective and safe to use socket timeout to accomplish that. 
> Follow is the changes need to do.
> 1. Replace `TCPSocket.open` with `Socket.tcp`
> 2. Create `TCPSocket` with `TCPSocket.for_fd`

I don't think this step should be necessary; Socket and
TCPSocket should be usable interchangeably for stream sockets
(maybe some API calls need to be changed, but I'd rather avoid
 the extra object entirely).

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>