How to set the time-out and capture timeout exceptions in Ruby Restclient

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.