First spit slot Baidu, is a pit, turned over Baidu did not turn to the answer, fortunately Google to force.
There are two methods, the first: bypass restclient directly using native methods:
Restclient::request.execute (: Method =:p ost,: url = = @url,: timeout = 90000000)
The advantage of this is that you don't have to rewrite the entire restclient module, you can use it directly, but I prefer the second approach, which is to rewrite the Restclient module as follows:
Require ' net/http '
Require ' OpenSSL '
Require ' Stringio '
Require ' URI '
Require ' zlib '
Require ' restclient '
Timeout_vaule = 30
Opentimeout_vaule = 30
Module RestClient2
# include Restclient
def self.get (URL, headers={}, &block)
Restclient::request.execute (: Method = =: get,: url = = URL,: headers = = headers,: timeout =>timeout_vaule,:o Pen_timeout =>opentimeout_vaule, &block)
End
def self.post (URL, payload, headers={}, &block)
Restclient::request.execute (: Method =:p ost,: url = = URL,:p ayload = payload,: headers = Headers,:timeout = >timeout_vaule,:open_timeout =>opentimeout_vaule, &block)
End
def self.patch (URL, payload, headers={}, &block)
Restclient::request.execute (: Method =:p atch,: url = = URL,:p ayload = payload,: headers = headers,:timeout =>timeout_vaule,:open_timeout =>opentimeout_vaule, &block)
End
def self.put (URL, payload, headers={}, &block)
Restclient::request.execute (: Method =:p ut,: url = = URL,:p ayload = payload,: headers = headers,:timeout =& Gt Timeout_vaule,:open_timeout =>opentimeout_vaule, &block)
End
def self.delete (URL, headers={}, &block)
Restclient::request.execute (: Method = =:d elete,: url = = URL,: headers = Headers,:timeout =>timeout_vaule, : Open_timeout =>opentimeout_vaule, &block)
End
def self.head (URL, headers={}, &block)
Restclient::request.execute (: Method =: Head,: url + = URL,: headers = headers,:timeout =>timeout_vaule,:o Pen_timeout =>opentimeout_vaule, &block)
End
def self.options (URL, headers={}, &block)
Restclient::request.execute (: Method =: options,: url + = URL,: headers = Headers,:timeout =>timeout_vaule, : Open_timeout =>opentimeout_vaule, &block)
End
End
This allows you to use it just as you would with a native restclient, but in this case, Ruby's own timeout is not very good, although it has not worked, so the exception that needs to be caught becomes: restclient:: Requesttimeout
Of course, this anomaly may have other captures, but this position seems to be quite accurate, so use this.
How to set the time-out and capture timeout exceptions in Ruby Restclient