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);
}
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")));
}
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);
}
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);
}
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;
}
Aggregations