You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, we don't have any retry logic present if API request fails with some network related exceptions in this line,
response = http.request(request)
we are using Net::HTTP library to make API request and recently it has been seen many request getting end of file reached exception which may be due to this issue.
If we have some retry logic around Client#send_request then it will help to handle these exceptions till some extent.
To add retry logic, we need to define a maximum number of retries, and then wrap the HTTP request in a loop that attempts to resend the request until either it succeeds or it hits the maximum number of retries. We may also want to add a sleep call between retries to prevent quickly exhausting your retry limit in the case of temporary network issues or server-side problems.
moduleTrolleyclassClient#...MAX_RETRIES=3# Maximum number of retries#...privatedefsend_request(endPoint,method,body='')# your original send_request logic hereuri=URI.parse(@config.api_base + endPoint)#...beginretries=0response=http.request(request)rescueStandardError=>eifretries < MAX_RETRIESretries += 1sleep(2**retries)# Exponential back-offretryelseraise"Request failed after #{MAX_RETRIES} attempts: #{e.message}"endendifresponse.code != '200' && response.code != '204'throw_status_code_exception(response.message + ' ' + response.body,response.code)endresponse.bodyend#...endend
Please note that:
The MAX_RETRIES constant is defined to specify the maximum number of retry attempts.
I've added a begin / rescue block to catch errors that occur during the request.
Inside the rescue block, we check if we've hit our maximum number of retries. If not, we increment the retry count, wait for a period of time (sleep(2**retries) provides an exponential back-off), and then retry the request. If we have hit our maximum number of retries, we raise an exception to alert the user that the request has ultimately failed.
We can adjust the MAX_RETRIES and sleep values to suit your specific needs.
The text was updated successfully, but these errors were encountered:
Currently, we don't have any retry logic present if API request fails with some network related exceptions in this line,
we are using Net::HTTP library to make API request and recently it has been seen many request getting
end of file reached
exception which may be due to this issue.If we have some retry logic around Client#send_request then it will help to handle these exceptions till some extent.
To add retry logic, we need to define a maximum number of retries, and then wrap the HTTP request in a loop that attempts to resend the request until either it succeeds or it hits the maximum number of retries. We may also want to add a sleep call between retries to prevent quickly exhausting your retry limit in the case of temporary network issues or server-side problems.
Please note that:
MAX_RETRIES
constant is defined to specify the maximum number of retry attempts.begin
/rescue
block to catch errors that occur during the request.rescue
block, we check if we've hit our maximum number of retries. If not, we increment the retry count, wait for a period of time (sleep(2**retries)
provides an exponential back-off), and then retry the request. If we have hit our maximum number of retries, we raise an exception to alert the user that the request has ultimately failed.MAX_RETRIES
andsleep
values to suit your specific needs.The text was updated successfully, but these errors were encountered: