Java Code Examples for io.github.resilience4j.ratelimiter.RateLimiterConfig#Builder

The following examples show how to use io.github.resilience4j.ratelimiter.RateLimiterConfig#Builder . 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: RateLimiterRegistry.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
private static RateLimiterConfig createRateLimiterConfig(FormulaRateLimiterConfig rateLimiterConfig) {
    if (rateLimiterConfig == null) {
        return null;
    }

    RateLimiterConfig.Builder rateLimiterConfigBuilder = RateLimiterConfig.custom();

    if (rateLimiterConfig.getThreshold() != null) {
        rateLimiterConfigBuilder.limitForPeriod(rateLimiterConfig.getThreshold());
    }

    // rateLimiterConfigBuilder.limitRefreshPeriod(Duration.ofMillis(1000)); // default 1s

    if (rateLimiterConfig.getTimeoutInMillis() != null) { // default 0
        rateLimiterConfigBuilder.timeoutDuration(Duration.ofMillis(rateLimiterConfig.getTimeoutInMillis()));
    }

    return rateLimiterConfigBuilder.build();
}
 
Example 2
Source File: RateLimiterConfigCustomizer.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
/**
 * A convenient method to create RateLimiterConfigCustomizer using {@link Consumer}
 *
 * @param instanceName the name of the instance
 * @param consumer     delegate call to Consumer when  {@link RateLimiterConfigCustomizer#customize(RateLimiterConfig.Builder)}
 *                     is called
 * @return Customizer instance
 */
static RateLimiterConfigCustomizer of(@NonNull String instanceName,
    @NonNull Consumer<RateLimiterConfig.Builder> consumer) {
    return new RateLimiterConfigCustomizer() {

        @Override
        public void customize(RateLimiterConfig.Builder builder) {
            consumer.accept(builder);
        }

        @Override
        public String name() {
            return instanceName;
        }
    };
}
 
Example 3
Source File: TokenBucketRateLimiterTest.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    limitsForPeriod = 2;
    RateLimiterConfig.Builder rateLimiterConfigBuilder = RateLimiterConfig.custom();
    rateLimiterConfigBuilder.limitForPeriod(limitsForPeriod);
    rateLimiterConfig = rateLimiterConfigBuilder.build();
}
 
Example 4
Source File: RateLimiterConfigurationProperties.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
private RateLimiterConfig buildRateLimiterConfig(RateLimiterConfig.Builder builder,
    @Nullable InstanceProperties instanceProperties,
    CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer,
    String instanceName) {
    if (instanceProperties == null) {
        return builder.build();
    }

    if (instanceProperties.getLimitForPeriod() != null) {
        builder.limitForPeriod(instanceProperties.getLimitForPeriod());
    }

    if (instanceProperties.getLimitRefreshPeriod() != null) {
        builder.limitRefreshPeriod(instanceProperties.getLimitRefreshPeriod());
    }

    if (instanceProperties.getTimeoutDuration() != null) {
        builder.timeoutDuration(instanceProperties.getTimeoutDuration());
    }

    if (instanceProperties.getWritableStackTraceEnabled() != null) {
        builder.writableStackTraceEnabled(instanceProperties.getWritableStackTraceEnabled());
    }
    compositeRateLimiterCustomizer.getCustomizer(instanceName).ifPresent(
        rateLimiterConfigCustomizer -> rateLimiterConfigCustomizer.customize(builder));
    return builder.build();
}
 
Example 5
Source File: RateLimiterConfigCustomizer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Customize rate limiter configuration builder.
 *
 * @param configBuilder to be customized
 */
void customize(RateLimiterConfig.Builder configBuilder);