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);
}
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();
}
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);
}
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());
}
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));
}
Aggregations