Search code examples
rubysocketstimeoutruby-2.5

ruby 2.5: set timeout on tcp socket's read


Ruby 2.5's doc says that for:
myTCPServer = Socket.tcp(host, port, nil, nil, :connect_timeout=60),
the only option it accepts is :connect_timeout.

(Ruby 2.0's doc may have a copy-paste error, claiming that :timeout is another option, but not saying what that option does. At any rate, that option doesn't work in 2.5.1p57 on Ubuntu 18.04 LTS.)

Is the gem tcp_timeout the best way to make a TCP socket whose connected session can timeout on read for a specified duration, e.g via mySession.gets?
(Not a timeout on connect. Not a timeout on write.)

Or IO::select?


Solution

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