Net Processing :: ReadTimeout Error in HTTParty

I am using httparty (0.13.1) gem. I am making a series of API calls using httparty. Some of my initial API calls succeeded, but subsequent calls are interrupted sequentially. I added a timeout of 180 seconds. I searched google but I cannot find a solution. Because of this, I have been afraid for a long time.

My code is:

response = HTTParty.get("http://pubapi.cryptsy.com/api.php?method=marketdatav2", timeout: 180)

Error:

A Net::ReadTimeout occurred in background at 2014-10-05 11:42:06 UTC :

I do not know if this timeout works? I feel that 180 seconds is more than enough to get an answer, because the default timeout is 60 seconds. If I want to handle a Net Net timeout error, is there a way to do this? I want to return zero if this error occurs. Or is there a better solution to avoid this error?

+4
source share
2 answers

Had a similar problem with another module, and in my case it will probably succeed if you try again, so I will catch the timeout and try again.

  max_retries = 3
  times_retried = 0

  begin
    #thing that errors sometimes

  rescue Net::ReadTimeout => error
    if times_retried < max_retries
      times_retried += 1
      puts "Failed to <do the thing>, retry #{times_retried}/#{max_retries}"
      retry
    else
      puts "Exiting script. <explanation of why this is unlikely to recover>"
      exit(1)
    end
  end

Left here if someone finds it useful.

+9
source

you can use rescueto handle timeout exceptions:

begin
  HTTParty.get("http://pubapi.cryptsy.com/api.php?method=marketdatav2", timeout: 180)
rescue Net::ReadTimeout
  nil
end
+4
source

All Articles