Java Code Examples for io.github.resilience4j.ratelimiter.RateLimiterRegistry#rateLimiter()

The following examples show how to use io.github.resilience4j.ratelimiter.RateLimiterRegistry#rateLimiter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 6 votes vote down vote up
public BackendBController(
        @Qualifier("backendBService")Service businessBService,
        CircuitBreakerRegistry circuitBreakerRegistry,
        ThreadPoolBulkheadRegistry threadPoolBulkheadRegistry,
        BulkheadRegistry bulkheadRegistry,
        RetryRegistry retryRegistry,
        RateLimiterRegistry rateLimiterRegistry,
        TimeLimiterRegistry timeLimiterRegistry){
    this.businessBService = businessBService;
    this.circuitBreaker = circuitBreakerRegistry.circuitBreaker(BACKEND_B);
    this.bulkhead = bulkheadRegistry.bulkhead(BACKEND_B);
    this.threadPoolBulkhead = threadPoolBulkheadRegistry.bulkhead(BACKEND_B);
    this.retry = retryRegistry.retry(BACKEND_B);
    this.rateLimiter = rateLimiterRegistry.rateLimiter(BACKEND_B);
    this.timeLimiter = timeLimiterRegistry.timeLimiter(BACKEND_B);
    this.scheduledExecutorService = Executors.newScheduledThreadPool(3);
}
 
Example 2
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: TaggedRateLimiterMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void customMetricNamesGetApplied() {
    MeterRegistry meterRegistry = new SimpleMeterRegistry();
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    rateLimiterRegistry.rateLimiter("backendA");
    TaggedRateLimiterMetrics.ofRateLimiterRegistry(
        RateLimiterMetricNames.custom()
            .availablePermissionsMetricName("custom_available_permissions")
            .waitingThreadsMetricName("custom_waiting_threads")
            .build(),
        rateLimiterRegistry
    ).bindTo(meterRegistry);

    Set<String> metricNames = meterRegistry.getMeters()
        .stream()
        .map(Meter::getId)
        .map(Meter.Id::getName)
        .collect(Collectors.toSet());

    assertThat(metricNames).hasSameElementsAs(Arrays.asList(
        "custom_available_permissions",
        "custom_waiting_threads"
    ));
}
 
Example 4
Source File: TaggedRateLimiterMetricsPublisherTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void customMetricNamesGetApplied() {
    MeterRegistry meterRegistry = new SimpleMeterRegistry();
    TaggedRateLimiterMetricsPublisher taggedRateLimiterMetricsPublisher = new TaggedRateLimiterMetricsPublisher(
        RateLimiterMetricNames.custom()
            .availablePermissionsMetricName("custom_available_permissions")
            .waitingThreadsMetricName("custom_waiting_threads")
            .build(), meterRegistry);

    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry
        .of(RateLimiterConfig.ofDefaults(), taggedRateLimiterMetricsPublisher);
    rateLimiterRegistry.rateLimiter("backendA");

    Set<String> metricNames = meterRegistry.getMeters()
        .stream()
        .map(Meter::getId)
        .map(Meter.Id::getName)
        .collect(Collectors.toSet());

    assertThat(metricNames).hasSameElementsAs(Arrays.asList(
        "custom_available_permissions",
        "custom_waiting_threads"
    ));
}
 
Example 5
Source File: RateLimiterMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected RateLimiter givenMetricRegistry(MetricRegistry metricRegistry) {
    RateLimiterRegistry rateLimiterRegistry =
        RateLimiterRegistry.of(RateLimiterConfig.ofDefaults(),
            new RateLimiterMetricsPublisher(metricRegistry));

    return rateLimiterRegistry.rateLimiter("testLimit");
}
 
Example 6
Source File: RateLimiterMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected RateLimiter givenMetricRegistry(String prefix, MetricRegistry metricRegistry) {
    RateLimiterRegistry rateLimiterRegistry =
        RateLimiterRegistry.of(RateLimiterConfig.ofDefaults(),
            new RateLimiterMetricsPublisher(prefix, metricRegistry));

    return rateLimiterRegistry.rateLimiter("testLimit");
}
 
Example 7
Source File: RateLimiterMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected RateLimiter givenMetricRegistry(MetricRegistry metricRegistry) {
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
    metricRegistry.registerAll(RateLimiterMetrics.ofRateLimiterRegistry(rateLimiterRegistry));

    return rateLimiter;
}
 
Example 8
Source File: RateLimiterMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected RateLimiter givenMetricRegistry(String prefix, MetricRegistry metricRegistry) {
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.ofDefaults();
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("testLimit");
    metricRegistry.registerAll(
        RateLimiterMetrics.ofIterable(prefix, rateLimiterRegistry.getAllRateLimiters()));

    return rateLimiter;
}
 
Example 9
Source File: RateLimiterConfigurationTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRateLimiterRegistry() {
    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    instanceProperties1.setLimitForPeriod(2);
    instanceProperties1.setSubscribeForEvents(true);
    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    instanceProperties2.setLimitForPeriod(4);
    instanceProperties2.setSubscribeForEvents(true);
    RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties();
    rateLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
    rateLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
    rateLimiterConfigurationProperties.setRateLimiterAspectOrder(300);
    RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration();
    DefaultEventConsumerRegistry<RateLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();

    RateLimiterRegistry rateLimiterRegistry = rateLimiterConfiguration
        .rateLimiterRegistry(rateLimiterConfigurationProperties, eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeRateLimiterCustomizerTest());

    assertThat(rateLimiterConfigurationProperties.getRateLimiterAspectOrder()).isEqualTo(300);
    assertThat(rateLimiterRegistry.getAllRateLimiters().size()).isEqualTo(2);
    RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter("backend1");
    assertThat(rateLimiter).isNotNull();
    assertThat(rateLimiter.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(2);
    RateLimiter rateLimiter2 = rateLimiterRegistry.rateLimiter("backend2");
    assertThat(rateLimiter2).isNotNull();
    assertThat(rateLimiter2.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(4);
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
 
Example 10
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterGetAllRateLimiters() {
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter("foo");

    assertThat(registry.getAllRateLimiters().size()).isEqualTo(1);
    assertThat(registry.getAllRateLimiters().get(0).getName()).isEqualTo("foo");
}
 
Example 11
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterNewWithNullConfigSupplier() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage("Supplier must not be null");
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    Supplier<RateLimiterConfig> rateLimiterConfigSupplier = null;
    registry.rateLimiter("name", rateLimiterConfigSupplier);
}
 
Example 12
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterNewWithNullNameAndConfigSupplier() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(NAME_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter(null, () -> config);
}
 
Example 13
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterNewWithNullNameAndNonDefaultConfig() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(NAME_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter(null, config);
}
 
Example 14
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterNewWithNullNonDefaultConfig() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(CONFIG_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    RateLimiterConfig rateLimiterConfig = null;
    registry.rateLimiter("name", rateLimiterConfig);
}
 
Example 15
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void rateLimiterNewWithNullName() throws Exception {
    exception.expect(NullPointerException.class);
    exception.expectMessage(NAME_MUST_NOT_BE_NULL);
    RateLimiterRegistry registry = new InMemoryRateLimiterRegistry(config);
    registry.rateLimiter(null);
}
 
Example 16
Source File: InMemoryRateLimiterRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 17
Source File: RateLimiterHandler.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
public RateLimiterHandler(RateLimiterRegistry rateLimiterRegistry, String rateLimiterName) {
    this.rateLimiter = rateLimiterRegistry.rateLimiter(rateLimiterName);
}
 
Example 18
Source File: RateLimiterConfigurationTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateRateLimiterRegistryWithSharedConfigs() {
    //Given
    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    defaultProperties.setLimitForPeriod(3);
    defaultProperties.setLimitRefreshPeriod(Duration.ofNanos(5000000));
    defaultProperties.setSubscribeForEvents(true);

    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    sharedProperties.setLimitForPeriod(2);
    sharedProperties.setLimitRefreshPeriod(Duration.ofNanos(6000000));
    sharedProperties.setSubscribeForEvents(true);

    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    backendWithDefaultConfig.setBaseConfig("default");
    backendWithDefaultConfig.setLimitForPeriod(200);
    backendWithDefaultConfig.setSubscribeForEvents(true);

    io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties();
    backendWithSharedConfig.setBaseConfig("sharedConfig");
    backendWithSharedConfig.setLimitForPeriod(300);
    backendWithSharedConfig.setSubscribeForEvents(true);

    RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties();
    rateLimiterConfigurationProperties.getConfigs().put("default", defaultProperties);
    rateLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);

    rateLimiterConfigurationProperties.getInstances()
        .put("backendWithDefaultConfig", backendWithDefaultConfig);
    rateLimiterConfigurationProperties.getInstances()
        .put("backendWithSharedConfig", backendWithSharedConfig);

    RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration();
    DefaultEventConsumerRegistry<RateLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();

    //When
    RateLimiterRegistry rateLimiterRegistry = rateLimiterConfiguration
        .rateLimiterRegistry(rateLimiterConfigurationProperties, eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeRateLimiterCustomizerTest());

    //Then
    assertThat(rateLimiterRegistry.getAllRateLimiters().size()).isEqualTo(2);

    // Should get default config and override LimitForPeriod
    RateLimiter rateLimiter1 = rateLimiterRegistry.rateLimiter("backendWithDefaultConfig");
    assertThat(rateLimiter1).isNotNull();
    assertThat(rateLimiter1.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(200);
    assertThat(rateLimiter1.getRateLimiterConfig().getLimitRefreshPeriod())
        .isEqualTo(Duration.ofMillis(5));

    // Should get shared config and override LimitForPeriod
    RateLimiter rateLimiter2 = rateLimiterRegistry.rateLimiter("backendWithSharedConfig");
    assertThat(rateLimiter2).isNotNull();
    assertThat(rateLimiter2.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(300);
    assertThat(rateLimiter2.getRateLimiterConfig().getLimitRefreshPeriod())
        .isEqualTo(Duration.ofMillis(6));

    // Unknown backend should get default config of Registry
    RateLimiter rerateLimiter3 = rateLimiterRegistry.rateLimiter("unknownBackend");
    assertThat(rerateLimiter3).isNotNull();
    assertThat(rerateLimiter3.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(3);

    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}