3

I am using the resilience4j-spring-boot2 library (io.github.resilience4j:resilience4j-spring-boot2, version 1.5.0) and Spring Boot version 2.3.1.RELEASE. I created a RateLimiter which should only let one thread execute a certain method every 30 seconds. However, there does not seem to be a boundary (except for configurated number of threads running in the Tomcat server) for threads to call the method.

I implemented a simple Service which waits for a given amount of time and then returns "Hello!":

@RequestMapping("/")
@RestController
public class ResilientService {

    @RateLimiter(name = "hello-rl")
    @GetMapping("/hello/{timeout}")
    public String hello(@PathVariable int timeout) throws InterruptedException {
        Thread.sleep(timeout);
        return "Hello!";
    }

}

And configured the Ratelimiter as follows

resilience4j.ratelimiter:
  instances:
    hello-rl:
      limitForPeriod: 1  # The number of permissions available during one limit refresh period
      limitRefreshPeriod: 30s  # The period of a limit refresh. After each period the rate limiter sets its permissions count back to the limitForPeriod value
      timeoutDuration: 30s  # The default wait time a thread waits for a permission

And I called the service with eight threads:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@Slf4j
public class CircuitBreakerTest {

    @Autowired
    TestRestTemplate testRestTemplate;

    @LocalServerPort
    private int port;

    @Value("${server.address}")
    private String serverAddress;

    @Test
    public void t() throws InterruptedException {
        final ExecutorService executorService = Executors.newFixedThreadPool(8);

        for (int i : IntStream.rangeClosed(1, 8).toArray()) {
            executorService.execute(() -> log.error(callService()));
        }

        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
    }

    private String callService() {
        return testRestTemplate.getForObject(
                "http://" + serverAddress + ":" + port + "/hello/5000",
                String.class
        );
    }

The output is as follows:

2020-08-07 10:41:10,095 ERROR [pool-1-thread-7] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-1] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-8] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-2] CircuitBreakerTest: Hello!
2020-08-07 10:41:10,095 ERROR [pool-1-thread-5] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,104 ERROR [pool-1-thread-3] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,121 ERROR [pool-1-thread-4] CircuitBreakerTest: Hello!
2020-08-07 10:41:15,121 ERROR [pool-1-thread-6] CircuitBreakerTest: Hello!

Only five threads seem to be allowed to execute the method at one time, but I don't know why. The Tomcat server is running with >8 threads. Did I make a mistake? Or am I misunderstanding how the RateLimiter is supposed to work?

GeckStar
  • 1,136
  • 1
  • 14
  • 22
  • 1
    Well if you debug it you will see that your properties are ignored and default config is taken into account. But even after I fixed that it still didn't work, so no answer :) – Shadov Aug 07 '20 at 09:36

1 Answers1

4

resilience4j-spring-boot2 uses aspects internally, so it is needed to add the spring-boot-starter-aop dependency to the project for the annotations to work:

        <dependency>
            <!-- for rate limiting -->
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
            <version>${resilience4j.version}</version>
        </dependency>
        <dependency>
            <!-- needed for Resilience4j -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

As stated in the official documentation right at the beginning ("Setup")... totally missed that.

GeckStar
  • 1,136
  • 1
  • 14
  • 22