Search in sources :

Example 1 with RateLimiter

use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.

the class InMemoryRateLimiterRegistryTest method rateLimiterPositiveWithSupplier.

@Test
@SuppressWarnings("unchecked")
public void rateLimiterPositiveWithSupplier() throws Exception {
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    Supplier<RateLimiterConfig> rateLimiterConfigSupplier = mock(Supplier.class);
    when(rateLimiterConfigSupplier.get()).thenReturn(config);
    RateLimiter firstRateLimiter = registry.rateLimiter("test", rateLimiterConfigSupplier);
    verify(rateLimiterConfigSupplier, times(1)).get();
    RateLimiter sameAsFirst = registry.rateLimiter("test", rateLimiterConfigSupplier);
    verify(rateLimiterConfigSupplier, times(1)).get();
    RateLimiter anotherLimit = registry.rateLimiter("test1", rateLimiterConfigSupplier);
    verify(rateLimiterConfigSupplier, times(2)).get();
    then(firstRateLimiter).isEqualTo(sameAsFirst);
    then(firstRateLimiter).isNotEqualTo(anotherLimit);
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 2 with RateLimiter

use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.

the class InMemoryRateLimiterRegistryTest method rateLimiterPositive.

@Test
public void rateLimiterPositive() throws Exception {
    RateLimiterRegistry registry = RateLimiterRegistry.of(config);
    RateLimiter firstRateLimiter = registry.rateLimiter("test");
    RateLimiter anotherLimit = registry.rateLimiter("test1");
    RateLimiter sameAsFirst = registry.rateLimiter("test");
    then(firstRateLimiter).isEqualTo(sameAsFirst);
    then(firstRateLimiter).isNotEqualTo(anotherLimit);
}
Also used : RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 3 with RateLimiter

use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.

the class SemaphoreBasedRateLimiterImplTest method changeLimitForPeriod.

@Test
public void changeLimitForPeriod() throws Exception {
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiter rateLimiter = new SemaphoreBasedRateLimiter("some", config, scheduledExecutorService);
    RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
    then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
    then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
    then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
    then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
    rateLimiter.changeLimitForPeriod(LIMIT * 2);
    then(rateLimiterConfig != rateLimiter.getRateLimiterConfig()).isTrue();
    rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
    then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
    then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT * 2);
    then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
    then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 4 with RateLimiter

use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.

the class RateLimiterExports method collect.

/**
 * {@inheritDoc}
 */
@Override
public List<MetricFamilySamples> collect() {
    final GaugeMetricFamily stats = new GaugeMetricFamily(name, "Rate Limiter Stats", asList("name", "param"));
    for (RateLimiter rateLimiter : rateLimitersSupplier.get()) {
        final RateLimiter.Metrics metrics = rateLimiter.getMetrics();
        stats.addMetric(asList(rateLimiter.getName(), "available_permissions"), metrics.getAvailablePermissions());
        stats.addMetric(asList(rateLimiter.getName(), "waiting_threads"), metrics.getNumberOfWaitingThreads());
    }
    return singletonList(stats);
}
Also used : RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily)

Example 5 with RateLimiter

use of io.github.resilience4j.ratelimiter.RateLimiter in project resilience4j by resilience4j.

the class RateLimiterMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() throws Throwable {
    // Given
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
    metricRegistry.registerAll(RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry));
    // 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("resilience4j.ratelimiter.testLimit.number_of_waiting_threads").getValue()).isEqualTo(0);
    assertThat(metricRegistry.getGauges().get("resilience4j.ratelimiter.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