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.

Use named configuration with `@RateLimiter` annotation

See original GitHub issue

Resilience4j version: 1.2.0 Java version: 11.0.5

Problem description:

There is no way to pragmatically customize rate limiter applied with @RateLimiter annotation.

I tried:

@Configuration
class RateLimiterConfiguration {
    @Bean
    public RateLimiterRegistry rateLimiterRegistry() {
        RateLimiterConfig defaultConfiguration = RateLimiterConfig.custom()
                                                                  // set some parameters
                                                                  .build();
        RateLimiterConfig customConfiguration  = RateLimiterConfig.custom()
                                                                  // set some parameters
                                                                  .build();

        RateLimiterRegistry registry = RateLimiterRegistry.of(defaultConfiguration);
        registry.addConfiguration("custom", customConfiguration);

        // tried...
        // registry.replace("custom", RateLimiter.of("custom", customConfiguration));

        return registry;
    }
}

@Component
class RateLimitedComponent {
    @RateLimiter(name = "custom")
    public void limitMe() {
    }
}

but because of this code: https://github.com/resilience4j/resilience4j/blob/6d2f870e6bcb4a09d346b9888adf25dd8a09b9f8/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterAspect.java#L109-L110

and then https://github.com/resilience4j/resilience4j/blob/6d2f870e6bcb4a09d346b9888adf25dd8a09b9f8/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterAspect.java#L142-L143

and then https://github.com/resilience4j/resilience4j/blob/6d2f870e6bcb4a09d346b9888adf25dd8a09b9f8/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/InMemoryRateLimiterRegistry.java#L137-L138

there is no way to customize RateLimiter in code (I know, it is still possible with properties).

What is your opinion on:

  1. Add parameter to @RateLimiter annotation - configurationName and use it to retrieve RateLimiter instance or
  2. Use name from @RateLimiter annotation and pass it to RateLimiterRegistry as configuration name in getOrCreateRateLimiter:
rateLimiterRegistry
            .rateLimiter(name, name);

Some additional logic is needed because missing configuration will throw: https://github.com/resilience4j/resilience4j/blob/6d2f870e6bcb4a09d346b9888adf25dd8a09b9f8/resilience4j-ratelimiter/src/main/java/io/github/resilience4j/ratelimiter/internal/InMemoryRateLimiterRegistry.java#L200-L204

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
brempuszcommented, Jan 8, 2020

@Romeh Thanks for the idea.

According to comment https://github.com/resilience4j/resilience4j/pull/784#issuecomment-571832388 *Customizers will be used instead so I am closing this issue.

0reactions
Romehcommented, Jan 3, 2020

@brempusz

Just small question , why we not just do the following which will allow to replace the configuration after spring context is initiated ?


@Component
public class TestCustomConfig {

	private final CircuitBreakerRegistry circuitBreakerRegistry;

	public TestCustomConfig(CircuitBreakerRegistry circuitBreakerRegistry) {
		this.circuitBreakerRegistry = circuitBreakerRegistry;
	}

	@EventListener
	public void onApplicationEvent(ContextRefreshedEvent event) {
		System.out.println("after spring context  init");
		circuitBreakerRegistry.replace("backendA", CircuitBreaker.of("backendA",CircuitBreakerConfig.custom().slidingWindowSize(100000).build()));
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Rate-Limiting with Spring Boot and Resilience4j - Reflectoring
Let's see how to use the various features available in the RateLimiter module. This mainly involves configuring the RateLimiter instance in the ...
Read more >
RateLimiter - resilience4j
Rate limiting is an imperative technique to prepare your API for scale and establish high availability and reliability of your service.
Read more >
Ratelimiter With Resilience4j Spring Boot2 - Vikas Verma
Configure rate limiter in the application.yml file ... We will add the @RateLimiter annotation, and pass the config name, fallback method ...
Read more >
How to call the @RateLimiter annotation inside an @Aspect ...
When pj.proceed() is called the actual logic is executed but the @ratelimiter annotation does not seem to be working as the number of...
Read more >
General rate-limiter usage
This example consists of a Spring Boot application utilizing resilience4j annotations. The application includes a REST API which responds to ...
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