简体   繁体   中英

How to retry twice an Http request for a failed response

I am trying to build a retry logic for http post request only when i get a fail string in response. I have written this small code logic for retry but I am looking for a better way of retrying. Both the code length wise and logic wise. This is the implementation I have :

int count= 2;
int wait = 2;//Picking this from config file
while(count>0) {
      response = HttpConnectionUtil.INSTANCE.executeHttpPostRequest(url, parameters);
      if(!response.contains("fail")){break;}
      retryCount--;
      Thread.sleep(wait*1000);
        }

It is a spring-boot project in java. Are there any annotations for retry in this specific case?

I think you need to pay attention to Kaan's comment.

Looking for the word "fail" in the response body is a flakey way of detecting a failure.

  • What if the server is in a state where it can't or won't respond with the expected failure string?
  • What if it succeeds with a response that includes the string (say) "failure is not an option"?

You should be doing the following:

  • checking the response code,
  • for 2xx responses you should also use a more reliable way to detect the failure case, and
  • using a retry strategy that takes account of the reason that the request failed.

On the last point, retrying a request that is bound to fail again is a bad idea. It makes your application slower, and it places unnecessary load on the server.

Also, there are cases where a server's response code may be telling you that it is overloaded and that you should "back off". If that is the case, blindly retrying N times with a fixed time interval is liable to make the server load worse. Possible codes that may indicate this are 429 and 503. The response may contain a Retry-After header ... telling you how long you should wait before retrying.


I have written this small code logic for retry but I am looking for a better way of retrying. Both the code length wise and logic wise.

I have addressed the logic-wise issue; see above.

The length-wise issue is (frankly) irrelevant.

  • If this functionality is intended to be used in production code, it is more important that it does the right thing, in the face of (at least) all predictable behaviors of the server.

  • If the length is a concern to you, that implies you intend to copy-pasting this code a lot of times through out your code base. Don't do that! Write the code once as a reusable method or class.


Are there any annotations for retry in this specific case?

There is the Spring Retry project, and there is a Baeldung tutorial for it. However, the model is to retry when an exception is thrown, so you would need to wrap your code in something that will detect the (retryable) failures and throw an exception. You would most likely need to write more code than you have already to make this approach work for you.


Followup

You commented:

All the things are already handled as I said ...

In that case, my answer would be that there is no need to change your code 1 . You cannot make it better or shorter. In my opinion.

Move on.


1 - I am referring to the real code ... not the fantasy version in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM