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