Search in sources :

Example 6 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry 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 7 with RateLimiterRegistry

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

the class RateLimiterExportsTest method testConstructors.

@Test
public void testConstructors() {
    final RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(RateLimiterConfig.ofDefaults());
    RateLimiterExports.ofIterable("boo_limiters", singleton(RateLimiter.ofDefaults("foo")));
    RateLimiterExports.ofRateLimiterRegistry("boo_limiters", registry);
    RateLimiterExports.ofSupplier("boo_limiters", () -> singleton(RateLimiter.ofDefaults("foo")));
    RateLimiterExports.ofIterable(singleton(RateLimiter.ofDefaults("foo")));
    RateLimiterExports.ofRateLimiterRegistry(registry);
    RateLimiterExports.ofSupplier(() -> singleton(RateLimiter.ofDefaults("foo")));
}
Also used : InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Test(org.junit.Test)

Example 8 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry 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)

Example 9 with RateLimiterRegistry

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

the class RateLimiterMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() {
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    rateLimiterRegistry.rateLimiter("testName");
    RateLimiterMetrics rateLimiterMetrics = RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry);
    rateLimiterMetrics.bindTo(meterRegistry);
    final List<String> metricNames = meterRegistry.getMeters().stream().map(Meter::getId).map(Meter.Id::getName).collect(Collectors.toList());
    final List<String> expectedMetrics = newArrayList("resilience4j.ratelimiter.testName.available_permissions", "resilience4j.ratelimiter.testName.number_of_waiting_threads");
    assertThat(metricNames).hasSameElementsAs(expectedMetrics);
}
Also used : Meter(io.micrometer.core.instrument.Meter) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Test(org.junit.Test)

Example 10 with RateLimiterRegistry

use of io.github.resilience4j.ratelimiter.RateLimiterRegistry 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)

Aggregations

RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)13 Test (org.junit.Test)12 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)5 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)3 InMemoryRateLimiterRegistry (io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry)2 Meter (io.micrometer.core.instrument.Meter)1 Bean (org.springframework.context.annotation.Bean)1