简体   繁体   English

Ruby超时在Rails中不起作用?

[英]Ruby timeout does not work in Rails?

I'm having an issue trying to get a timeout when connecting via TCPSocket to a remote resource that isn't available. 当通过TCPSocket连接到不可用的远程资源时,尝试获取超时时遇到问题。 It just hangs indefinitely without timing out. 它只是无限期地挂起而不会超时。 Ideally I'd want it to try reconnect every 2 minutes or so, but the TCPSocket.new call seems to block. 理想情况下,我希望它每2分钟左右尝试重新连接,但是TCPSocket.new调用似乎阻塞了。 I've tried using timeout() but that doesn't do anything either. 我试过使用timeout(),但是那也不做任何事。 Trying the same call in an IRB instance works perfectly fine, but when it's in Rails, it fails. 在IRB实例中尝试相同的调用非常正常,但是在Rails中失败。 Anyone have a work around for this? 有人为此工作吗?

My code looks something as follows: 我的代码如下所示:

def self.connect!
  @@connection = TCPSocket.new IP, 4449
end

def self.send(cmd)
puts "send  "
unless @@connection
  self.connect!
end

loop do
  begin
    @@connection.puts(cmd)
    return
  rescue IOError
    sleep(self.get_reconnect_delay)
    self.connect!
  end
end
end

Unfortunately, there is currently no way to set timeouts on TCPSocket directly. 不幸的是,当前无法直接在TCPSocket上设置超时。

See http://bugs.ruby-lang.org/issues/5101 for the feature request. 有关功能请求,请参见http://bugs.ruby-lang.org/issues/5101 You will have use the basic Socket class and set socket options. 您将使用基本的Socket类并设置套接字选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM