Search in sources :

Example 16 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiter method changeLimitForPeriod.

/**
 * {@inheritDoc}
 */
@Override
public void changeLimitForPeriod(int limitForPeriod) {
    RateLimiterConfig newConfig = RateLimiterConfig.from(rateLimiterConfig.get()).limitForPeriod(limitForPeriod).build();
    rateLimiterConfig.set(newConfig);
}
Also used : RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig)

Example 17 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiterImplTest method rateLimiterCreationWithProvidedScheduler.

@Test
public void rateLimiterCreationWithProvidedScheduler() throws Exception {
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiterConfig configSpy = spy(config);
    SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", configSpy, scheduledExecutorService);
    ArgumentCaptor<Runnable> refreshLimitRunnableCaptor = ArgumentCaptor.forClass(Runnable.class);
    verify(scheduledExecutorService).scheduleAtFixedRate(refreshLimitRunnableCaptor.capture(), eq(config.getLimitRefreshPeriod().toNanos()), eq(config.getLimitRefreshPeriod().toNanos()), eq(TimeUnit.NANOSECONDS));
    Runnable refreshLimitRunnable = refreshLimitRunnableCaptor.getValue();
    then(limit.getPermission(ZERO)).isTrue();
    then(limit.getPermission(ZERO)).isTrue();
    then(limit.getPermission(ZERO)).isFalse();
    Thread.sleep(REFRESH_PERIOD.toMillis() * 2);
    verify(configSpy, times(1)).getLimitForPeriod();
    refreshLimitRunnable.run();
    verify(configSpy, times(2)).getLimitForPeriod();
    then(limit.getPermission(ZERO)).isTrue();
    then(limit.getPermission(ZERO)).isTrue();
    then(limit.getPermission(ZERO)).isFalse();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) Test(org.junit.Test)

Example 18 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiterImplTest method getPermissionAndMetrics.

@Test
public void getPermissionAndMetrics() throws Exception {
    ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
    RateLimiterConfig configSpy = spy(config);
    SemaphoreBasedRateLimiter limit = new SemaphoreBasedRateLimiter("test", configSpy, scheduledExecutorService);
    RateLimiter.Metrics detailedMetrics = limit.getMetrics();
    SynchronousQueue<Object> synchronousQueue = new SynchronousQueue<>();
    Thread thread = new Thread(() -> {
        run(() -> {
            for (int i = 0; i < LIMIT; i++) {
                synchronousQueue.put(O);
                limit.getPermission(TIMEOUT);
            }
            limit.getPermission(TIMEOUT);
        });
    });
    thread.setDaemon(true);
    thread.start();
    for (int i = 0; i < LIMIT; i++) {
        synchronousQueue.take();
    }
    awaitImpatiently().atMost(100, TimeUnit.MILLISECONDS).until(detailedMetrics::getAvailablePermissions, equalTo(0));
    awaitImpatiently().atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TIMED_WAITING));
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(0);
    limit.refreshLimit();
    awaitImpatiently().atMost(100, TimeUnit.MILLISECONDS).until(detailedMetrics::getAvailablePermissions, equalTo(1));
    awaitImpatiently().atMost(2, TimeUnit.SECONDS).until(thread::getState, equalTo(TERMINATED));
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(1);
    limit.changeLimitForPeriod(3);
    limit.refreshLimit();
    then(detailedMetrics.getAvailablePermissions()).isEqualTo(3);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SynchronousQueue(java.util.concurrent.SynchronousQueue) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Example 19 with RateLimiterConfig

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

the class SemaphoreBasedRateLimiterImplTest method changeDefaultTimeoutDuration.

@Test
public void changeDefaultTimeoutDuration() 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.changeTimeoutDuration(Duration.ofSeconds(1));
    then(rateLimiterConfig != rateLimiter.getRateLimiterConfig()).isTrue();
    rateLimiterConfig = rateLimiter.getRateLimiterConfig();
    then(rateLimiterConfig.getTimeoutDuration()).isEqualTo(Duration.ofSeconds(1));
    then(rateLimiterConfig.getTimeoutDurationInNanos()).isEqualTo(Duration.ofSeconds(1).toNanos());
    then(rateLimiterConfig.getLimitForPeriod()).isEqualTo(LIMIT);
    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 20 with RateLimiterConfig

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

the class AtomicRateLimiter method changeLimitForPeriod.

/**
 * {@inheritDoc}
 */
@Override
public void changeLimitForPeriod(final int limitForPeriod) {
    RateLimiterConfig newConfig = RateLimiterConfig.from(state.get().config).limitForPeriod(limitForPeriod).build();
    state.updateAndGet(currentState -> new State(newConfig, currentState.activeCycle, currentState.activePermissions, currentState.nanosToWait));
}
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