Search in sources :

Example 6 with RateLimiter

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;
}
Also used : RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter)

Example 7 with 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;
}
Also used : InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Bean(org.springframework.context.annotation.Bean)

Example 8 with RateLimiter

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);
}
Also used : RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)

Example 9 with RateLimiter

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();
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 10 with RateLimiter

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);
}
Also used : RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Aggregations

RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)15 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)9 Test (org.junit.Test)8 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)5 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)2 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)2 RateLimiterEvent (io.github.resilience4j.ratelimiter.event.RateLimiterEvent)2 InMemoryRateLimiterRegistry (io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry)2 Seq (io.vavr.collection.Seq)2 Comparator (java.util.Comparator)2 List (java.util.List)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ReactorAdapter.toFlux (io.github.resilience4j.adapter.ReactorAdapter.toFlux)1 RxJava2Adapter (io.github.resilience4j.adapter.RxJava2Adapter)1 MetricUtils.getName (io.github.resilience4j.micrometer.MetricUtils.getName)1 AtomicRateLimiter (io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)1 RateLimiterEventDTO (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventDTO)1 RateLimiterEventsEndpointResponse (io.github.resilience4j.ratelimiter.monitoring.model.RateLimiterEventsEndpointResponse)1 AVAILABLE_PERMISSIONS (io.github.resilience4j.ratelimiter.utils.MetricNames.AVAILABLE_PERMISSIONS)1 DEFAULT_PREFIX (io.github.resilience4j.ratelimiter.utils.MetricNames.DEFAULT_PREFIX)1