question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Spring Boot Servlet Implementation with @Timelimiter

See original GitHub issue

Resilience4j version: 1.5.0

Java version: 1.8

Hi,

we still use the Servlet part and not yet the Webflux part of Spring Boot, but still want to benefit from the @TimeLimiter annotation. The code of the TimeLimiterAspect enforces to use a CompletionStage as return type in case there is no suitable TimeLimiterAspectExt available. Since we’re using the servlet implementation methods, which are annotated with the @TimeLimiter annotation used to be blocking and therefore simply returned the desired type directly.

Now to archive the same goal we simply return a CompletableFuture<MyType> and the caller of this method calls get() on the returned CompletableFuture<MyType>. Would this be the right approach? Or to you see any undesired side effects when using the @TimeLimiter annotation like this? I am asking this because sometimes we face some delays when calling other rest APIs by using Timelimiter, and I am afraid that the timelimiter threading overhead causes these delays?

Unfortunately it is not really easy to make blocking code non blocking.

Thanks in advance for an answer to all these questions 😉

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
RobWincommented, Jun 19, 2020

After your comment GitHub was down. Is there a correlation? 😛

1reaction
SimonScholzcommented, Jun 19, 2020

For others, who might also want to use the blocking part of the timelimiter: I introduced a new annotation similar to the io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker annotation and also made use of most of the parts of the io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect and simply added a TimeLimiter and Bulkhead to it like so:

        FallbackMethod fallbackMethod =
                FallbackMethod.create(fallbackMethodValue, method, proceedingJoinPoint.getArgs(), proceedingJoinPoint.getTarget());
        return fallbackDecorators.decorate(fallbackMethod, () -> proceed(proceedingJoinPoint, circuitBreaker, bulkhead, timelimiter)).apply();
    }

    private Object proceed(ProceedingJoinPoint proceedingJoinPoint, CircuitBreaker circuitBreaker, Bulkhead bulkhead, TimeLimiter timeLimiter)
            throws Throwable {
        return circuitBreaker.executeCallable(timeLimiter.decorateFutureSupplier(() -> CompletableFuture.supplyAsync(() -> {
            try {
                return bulkhead.executeCheckedSupplier(proceedingJoinPoint::proceed);
            } catch (Throwable throwable) {
                throw new CompletionException(throwable);
            }
        })));
    }

Hope that helps someone else with similar demands 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timeouts with Spring Boot and Resilience4j - Reflectoring
Continuing the Resilience4j journey, this article on Spring Boot TimeLimiter shows when and how to use it to build resilient applications.
Read more >
Top 50 Spring Boot Interview Questions and Answers in 2023
This article on Top 50 Spring Boot Interview Questions is a comprehensive guide to the most frequently asked questions in your interviews.
Read more >
Circuit Breaker (Resilience4j) not working in Spring Boot app
I am trying to implement circuit breaker in my spring boot app. I made 2 bare minimum microservices to test the implementation of...
Read more >
SpringBoot Resilience4j Timelimiter | Timelimiter pattern
You will learn how we can build applications using Resilience4j- Timelimiter implementation in Spring Boot.
Read more >
Setting a Request Timeout for a Spring REST API - Baeldung
The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found