Search in sources :

Example 6 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiterImplTest method changeLimitForPeriod.

@Test
public void changeLimitForPeriod() throws Exception {
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiter rateLimiter = new SemaphoreBasedRateLimiter("some", config, scheduledExecutorService);
    RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
    then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
    then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
    then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
    then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
    rateLimiter.changeLimitForPeriod(LIMIT * 2);
    then(rateLimiterConfig != rateLimiter.getRateLimiterConfig()).isTrue();
    rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(TIMEOUT);
    then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(TIMEOUT.toNanos());
    then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT * 2);
    then(rateLimiterConfig.getLimitRefreshPeriod()).isEqualTo(REFRESH_PERIOD);
    then(rateLimiterConfig.getLimitRefreshPeriodInNanos()).isEqualTo(REFRESH_PERIOD.toNanos());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 7 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiterImplTest method getPermissionInterruption.

@Test
public void getPermissionInterruption() throws Exception {
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiterConfig configSpy = spy(config);
    SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", configSpy, scheduledExecutorService);
    limit.getPermission(ZERO);
    limit.getPermission(ZERO);
    Thread thread = new Thread(() -> {
        limit.getPermission(TIMEOUT);
        while (true) {
            Function.identity().apply(1);
        }
    });
    thread.setDaemon(true);
    thread.start();
    awaitImpatiently().atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TIMED_WAITING));
    thread.interrupt();
    awaitImpatiently().atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(RUNNABLE));
    awaitImpatiently().atMost(100, TimeUnit.MILLISECONDS).until(thread::isInterrupted);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) Test(org.junit.Test)

Example 8 with RateLimiterConfig

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

the class AtomicRateLimiter method changeTimeoutDuration.

/**
 * {@inheritDoc}
 */
@Override
public void changeTimeoutDuration(final Duration timeoutDuration) {
    RateLimiterConfig newConfig = RateLimiterConfig.from(state.get().config).timeoutDuration(timeoutDuration).build();
    state.updateAndGet(currentState -> new State(newConfig, currentState.activeCycle, currentState.activePermissions, currentState.nanosToWait));
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig)

Example 9 with RateLimiterConfig

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

the class InMemoryRateLimiterRegistry method rateLimiter.

/**
 * {@inheritDoc}
 */
@Override
public RateLimiter rateLimiter(final String name, final Supplier<RateLimiterConfig> rateLimiterConfigSupplier) {
    requireNonNull(name, NAME_MUST_NOT_BE_NULL);
    requireNonNull(rateLimiterConfigSupplier, SUPPLIER_MUST_NOT_BE_NULL);
    return rateLimiters.computeIfAbsent(name, limitName -> {
        RateLimiterConfig rateLimiterConfig = rateLimiterConfigSupplier.get();
        requireNonNull(rateLimiterConfig, CONFIG_MUST_NOT_BE_NULL);
        return new AtomicRateLimiter(limitName, rateLimiterConfig);
    });
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig)

Example 10 with RateLimiterConfig

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

the class RateLimiterAspect method getOrCreateRateLimiter.

private io.github.resilience4j.ratelimiter.RateLimiter getOrCreateRateLimiter(String methodName, String name) {
    io.github.resilience4j.ratelimiter.RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter(name);
    if (logger.isDebugEnabled()) {
        RateLimiterConfig rateLimiterConfig = rateLimiter.getRateLimiterConfig();
        logger.debug(RATE_LIMITER_RECEIVED, name, rateLimiterConfig.getLimitRefreshPeriod(), rateLimiterConfig.getLimitForPeriod(), rateLimiterConfig.getTimeoutDuration(), methodName);
    }
    return rateLimiter;
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig)

Aggregations

RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)20 Test (org.junit.Test)12 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)4 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)2 Duration (java.time.Duration)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Inject (com.google.inject.Inject)1 AtomicRateLimiter (io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)1 RateLimiterOperator (io.github.resilience4j.ratelimiter.operator.RateLimiterOperator)1 RecoveryFunction (io.github.resilience4j.ratpack.recovery.RecoveryFunction)1 Flowable (io.reactivex.Flowable)1 Observable (io.reactivex.Observable)1 Single (io.reactivex.Single)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1