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

The following examples show how to use io.github.resilience4j.ratelimiter.RateLimiterRegistry#of() . 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: 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 2
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 3
Source File: TaggedRateLimiterMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    meterRegistry = new SimpleMeterRegistry();
    taggedRateLimiterMetricsPublisher = new TaggedRateLimiterMetricsPublisher(meterRegistry);
    rateLimiterRegistry = RateLimiterRegistry
        .of(RateLimiterConfig.ofDefaults(), taggedRateLimiterMetricsPublisher);

    rateLimiter = rateLimiterRegistry.rateLimiter("backendA");
}
 
Example 4
Source File: Resilience4jModule.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public RateLimiterRegistry get() {
    // build configs
    RateLimiterConfigurationProperties rateLimiterProperties = resilience4jConfig
        .getRatelimiter();
    Map<String, RateLimiterConfig> configs = rateLimiterProperties.getConfigs()
        .entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
            entry -> rateLimiterProperties.createRateLimiterConfig(entry.getValue(),
                new CompositeCustomizer<RateLimiterConfigCustomizer>(
                    Collections.emptyList()),
                entry.getKey())));
    RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.of(configs);

    // build ratelimiters
    EndpointsConfig endpointsConfig = resilience4jConfig.getEndpoints();
    rateLimiterProperties.getInstances().forEach((name, rateLimiterConfig) -> {
        io.github.resilience4j.ratelimiter.RateLimiter rateLimiter =
            rateLimiterRegistry.rateLimiter(name,
                rateLimiterProperties.createRateLimiterConfig(rateLimiterConfig,
                    new CompositeCustomizer<>(Collections.emptyList()), name));
        if (endpointsConfig.getRatelimiter().isEnabled()) {
            rateLimiter.getEventPublisher().onEvent(eventConsumerRegistry
                .createEventConsumer(name,
                    rateLimiterConfig.getEventConsumerBufferSize() != null
                        ? rateLimiterConfig.getEventConsumerBufferSize() : 100));
        }
    });

    return rateLimiterRegistry;
}
 
Example 5
Source File: RateLimiterConfiguration.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes a rate limiter registry.
 *
 * @param rateLimiterConfigurationProperties The rate limiter configuration properties.
 * @param compositeRateLimiterCustomizer the composite rate limiter customizer delegate
 * @return a RateLimiterRegistry
 */
private RateLimiterRegistry createRateLimiterRegistry(
    RateLimiterConfigurationProperties rateLimiterConfigurationProperties,
    RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer,
    CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) {
    Map<String, RateLimiterConfig> configs = rateLimiterConfigurationProperties.getConfigs()
        .entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
            entry -> rateLimiterConfigurationProperties
                .createRateLimiterConfig(entry.getValue(), compositeRateLimiterCustomizer,
                    entry.getKey())));

    return RateLimiterRegistry.of(configs, rateLimiterRegistryEventConsumer,
        io.vavr.collection.HashMap.ofAll(rateLimiterConfigurationProperties.getTags()));
}
 
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: 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 8
Source File: RateLimiterConfigurationOnMissingBeanTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
@Bean
public RateLimiterRegistry rateLimiterRegistry() {
    rateLimiterRegistry = RateLimiterRegistry.of(RateLimiterConfig.ofDefaults());
    return rateLimiterRegistry;
}