use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterAutoConfiguration method createRateLimiter.
private RateLimiter createRateLimiter(RateLimiterRegistry rateLimiterRegistry, String name, RateLimiterProperties.LimiterProperties properties) {
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter(name, createRateLimiterConfig(properties));
logger.debug("Autoconfigure Rate Limiter registered. {}", rateLimiter);
return rateLimiter;
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterAutoConfiguration method rateLimiterRegistry.
@Bean
public RateLimiterRegistry rateLimiterRegistry(RateLimiterProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, ConfigurableBeanFactory beanFactory) {
RateLimiterRegistry rateLimiterRegistry = new InMemoryRateLimiterRegistry(RateLimiterConfig.ofDefaults());
rateLimiterProperties.getLimiters().forEach((name, properties) -> {
RateLimiter rateLimiter = createRateLimiter(rateLimiterRegistry, name, properties);
if (properties.getSubscribeForEvents()) {
subscribeToLimiterEvents(rateLimiterEventsConsumerRegistry, name, properties, rateLimiter);
}
if (properties.getRegisterHealthIndicator()) {
createHealthIndicatorForLimiter(beanFactory, name, rateLimiter);
}
});
return rateLimiterRegistry;
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterHealthIndicator method health.
@Override
public Health health() {
RateLimiter.Metrics metrics = rateLimiter.getMetrics();
int availablePermissions = metrics.getAvailablePermissions();
int numberOfWaitingThreads = metrics.getNumberOfWaitingThreads();
if (availablePermissions > 0 || numberOfWaitingThreads == 0) {
return rateLimiterHealth(Status.UP, availablePermissions, numberOfWaitingThreads);
}
if (rateLimiter instanceof AtomicRateLimiter) {
AtomicRateLimiter atomicRateLimiter = (AtomicRateLimiter) this.rateLimiter;
AtomicRateLimiter.AtomicRateLimiterMetrics detailedMetrics = atomicRateLimiter.getDetailedMetrics();
if (detailedMetrics.getNanosToWait() > timeoutInNanos) {
return rateLimiterHealth(Status.DOWN, availablePermissions, numberOfWaitingThreads);
}
}
return rateLimiterHealth(Status.UNKNOWN, availablePermissions, numberOfWaitingThreads);
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class DecoratorsTest method testDecoratorBuilderWithRateLimiter.
@Test
public void testDecoratorBuilderWithRateLimiter() {
// Given the HelloWorldService returns Hello world
given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
// Create a custom RateLimiter configuration
RateLimiterConfig config = RateLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).limitRefreshPeriod(Duration.ofSeconds(1)).limitForPeriod(1).build();
// Create a RateLimiter
RateLimiter rateLimiter = RateLimiter.of("backendName", config);
CheckedFunction0<String> restrictedSupplier = Decorators.ofCheckedSupplier(() -> helloWorldService.returnHelloWorld()).withRateLimiter(rateLimiter).decorate();
alignTime(rateLimiter);
Try<String> firstTry = Try.of(restrictedSupplier);
assertThat(firstTry.isSuccess()).isTrue();
Try<String> secondTry = Try.of(restrictedSupplier);
assertThat(secondTry.isFailure()).isTrue();
assertThat(secondTry.getCause()).isInstanceOf(RequestNotPermitted.class);
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}
use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.
the class RateLimiterMetricsTest method shouldUseCustomPrefix.
@Test
public void shouldUseCustomPrefix() throws Throwable {
// Given
RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
metricRegistry.registerAll(RateLimiterMetrics.ofIterable("testPre", rateLimiterRegistry.getAllRateLimiters()));
// Given the HelloWorldService returns Hello world
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
// When
String value = rateLimiter.executeSupplier(helloWorldService::returnHelloWorld);
// Then
assertThat(value).isEqualTo("Hello world");
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
assertThat(metricRegistry.getMetrics()).hasSize(2);
assertThat(metricRegistry.getGauges().get("testPre.testLimit.number_of_waiting_threads").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("testPre.testLimit.available_permissions").getValue()).isIn(DEFAULT_LIMIT_FOR_PERIOD, DEFAULT_LIMIT_FOR_PERIOD - 1);
}
Aggregations