Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry on HTTP Status code (eg: 429, 500, 502, 503, 504) in Helidon REST API #8703

Closed
sammee opened this issue Apr 25, 2024 · 4 comments
Closed
Assignees
Labels
enhancement New feature or request fault-tolerance MP P3 wontfix This will not be worked on
Projects

Comments

@sammee
Copy link

sammee commented Apr 25, 2024

Hi Team,
we need to implement retry on HTTP Status code(eg: for 429, 500, 502, 503, 504) for API call. Is there any library in Helidon(using 3.2) or any other library which we can use to have retry on these status code? Retry can be both linear and exponential too. I checked the faultTolerance lib in helidon and it provides annotations based retry but it only retries based on exception type.

If there is no such library, Could this be implemented in future releases of helidon ?
@Retry(maxRetries=3, delay=1000, retryOn={Exception.class})

Thanks,
Sammee

@github-actions github-actions bot added this to Triage in Backlog Apr 25, 2024
@m0mus m0mus moved this from Triage to Normal priority in Backlog Apr 25, 2024
@spericas
Copy link
Member

spericas commented May 6, 2024

There's one way to support a similar use case using existing features in Rest Client and Fault Tolerance. Rest Client has the notion of a ResponseExceptionMapper, which can be used to map a Response to an Exception. For example,

    public class MyResponseMapper implements ResponseExceptionMapper<MyException> {

        @Override
        public MyException toThrowable(Response response) {
            if (response.getStatus() != 200) {
                System.out.println("Throwing exception");
                throw new MyException(response.getStatus());
            }
            return null;
        }
    }

In this example, and mainly for simplicity, any status code other than 200 is mapped to an instance of MyException (naturally, different error codes could be mapped to different exception types). This mapper can be registered as a provider in a client and used with the normal FT @Retry:

    @Path("/greet")
    @RegisterRestClient(baseUri="http://localhost:8080")
    @RegisterProvider(MyResponseMapper.class)
    public interface GreetResourceClient {

        @GET
        @Retry(retryOn = { MyException.class })
        @Produces(MediaType.APPLICATION_JSON)
        Response getDefaultMessage();
    }

Thus, any time MyException is thrown, the exception will be caught by the FT handler and the method retried up to 3 times (the default for that annotation).

I understand this may not be as convenient as having a dedicated annotation for this purpose, but it is possible using existing constructs and extensions.

@spericas
Copy link
Member

@sammee Have you tried the proposed alternative?

@sammee
Copy link
Author

sammee commented May 14, 2024

@spericas I have implemented a variant of this. Using ResponseMapper will throw a custom exception for a particular status code. This will be applied for all Rest clients. Instead, I wanted to have retry on some specific api. So i have used @Retry annotation on the method where the api is called and throw a custom exception when the status-code or error is retryable.
Thanks for the suggestion.

@spericas spericas added the wontfix This will not be worked on label May 14, 2024
@spericas
Copy link
Member

I gather from the comments that the workaround or alternative is satisfactory. Closing as won't fix for now.

Backlog automation moved this from Normal priority to Closed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fault-tolerance MP P3 wontfix This will not be worked on
Projects
Backlog
  
Closed
Development

No branches or pull requests

3 participants