How Is Ruby Tcpsocket Timeout Defined

How is Ruby TCPSocket timeout defined?

Use begin .. rescue Errno::ETIMEDOUT to catch the timeout:

require 'socket'

begin
TCPSocket.new('www.example.com', 111)
rescue Errno::ETIMEDOUT
p 'timeout'
end

To catch any socket exceptions, use SystemCallError instead.

According to the SystemCallError documentation:

SystemCallError is the base class for all low-level platform-dependent errors.

The errors available on the current platform are subclasses of
SystemCallError and are defined in the Errno module.


TCPSocket.new does not support timeout directly.

Use Socket::connect_non_blocking and IO::select to set timeout.

require 'socket'

def connect(host, port, timeout = 5)

# Convert the passed host into structures the non-blocking calls
# can deal with
addr = Socket.getaddrinfo(host, nil)
sockaddr = Socket.pack_sockaddr_in(port, addr[0][4])

Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

begin
# Initiate the socket connection in the background. If it doesn't fail
# immediatelyit will raise an IO::WaitWritable (Errno::EINPROGRESS)
# indicating the connection is in progress.
socket.connect_nonblock(sockaddr)

rescue IO::WaitWritable
# IO.select will block until the socket is writable or the timeout
# is exceeded - whichever comes first.
if IO.select(nil, [socket], nil, timeout)
begin
# Verify there is now a good connection
socket.connect_nonblock(sockaddr)
rescue Errno::EISCONN
# Good news everybody, the socket is connected!
rescue
# An unexpected exception was raised - the connection is no good.
socket.close
raise
end
else
# IO.select returns nil when the socket is not ready before timeout
# seconds have elapsed
socket.close
raise "Connection timeout"
end
end
end
end

connect('www.example.com', 111, 2)

The above code comes from "Setting a Socket Connection Timeout in Ruby".

ruby 2.5: set timeout on tcp socket's read

Yes, IO::select, using recvfrom instead of gets (which might wait a while for a newline).

How do I set the socket timeout in Ruby?

The solution I found which appears to work is to use Timeout::timeout:

require 'timeout'
...
begin
timeout(5) do
message, client_address = some_socket.recvfrom(1024)
end
rescue Timeout::Error
puts "Timed out!"
end

Socket timeout in Ruby

I have tested and my ISP has something to do with the time limitation issues of my longer connections.

Ruby timeout does not work in Rails?

Unfortunately, there is currently no way to set timeouts on TCPSocket directly.

See http://bugs.ruby-lang.org/issues/5101 for the feature request. You will have use the basic Socket class and set socket options.

How to configure read_timeout and open_timeout for Selenium in Ruby?

You can refer to Documentation Ruby-Bindings : Internal timeouts

From Documentation : Internal timeouts Internally, WebDriver uses
HTTP to communicate with a lot of the drivers (the JsonWireProtocol).
By default, Net::HTTP from Ruby's standard library is used, which has
a default timeout of 60 seconds. If you call e.g. Driver#get,
Driver#click on a page that takes more than 60 seconds to load, you'll
see a Timeout::Error raised from Net::HTTP. You can configure this
timeout (before launching a browser) by doing:

  client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 120 # seconds
driver = Selenium::WebDriver.for :remote, http_client: client

In Watir it may be something like

caps = Selenium::WebDriver::Remote::Capabilities.chrome          
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 600
client.open_timeout = 600
driver = Watir::Browser.new :chrome, :desired_capabilities => caps,
:http_client => client


Related Topics



Leave a reply



Submit