HTTPClient :: ReceiveTimeoutError in Ruby on Rails

Hi, I am using the HTTPClient gem and I have a problem; I use it to register users. When starting, the username and password are inserted into my database, but it waits a long time and returns to me.

HTTP Client::ReceiveTimeout Error in User Controller#signup execution expired.

How can I fix my problem?

+5
source share
3 answers

I think your http connection or remote connection is unstable. Whenever I have this situation, I use a timeout and try again several times. The following code will execute your code for 10 seconds, and if it is not completed, it will time out and try again. If the retry is not performed many times, it will ultimately fail.

def timeout_and_retry
  retries = 0
  begin
    Timeout::timeout(10) { yield }
  rescue Timeout::Error
    raise if (self.retries += 1) > 3
    retry
  end
end

timeout_and_retry do
   # http client get code goes here
end
+5

receive_timeout;

client = HTTPClient.new
client.receive_timeout = 10000    # setting receivetimeout for HTTPClient!
+11

When you receive HTTP: ReceiveTimeOutError, this means that the request has been sent and it has waited for a certain time, and since there is no response, it will expire during this time, and you will get this error.

Always indicate more time so that it waits more for a response.

0
source

All Articles