use of io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter in project resilience4j by resilience4j.
the class RateLimiterBenchmark method setUp.
@Setup
public void setUp() {
RateLimiterConfig rateLimiterConfig = RateLimiterConfig.custom().limitForPeriod(Integer.MAX_VALUE).limitRefreshPeriod(Duration.ofNanos(10)).timeoutDuration(Duration.ofSeconds(5)).build();
semaphoreBasedRateLimiter = new SemaphoreBasedRateLimiter("semaphoreBased", rateLimiterConfig);
atomicRateLimiter = new AtomicRateLimiter("atomicBased", rateLimiterConfig);
Supplier<String> stringSupplier = () -> {
Blackhole.consumeCPU(1);
return "Hello Benchmark";
};
semaphoreGuardedSupplier = RateLimiter.decorateSupplier(semaphoreBasedRateLimiter, stringSupplier);
atomicGuardedSupplier = RateLimiter.decorateSupplier(atomicRateLimiter, stringSupplier);
}
use of io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter in project resilience4j by resilience4j.
the class RateLimiterHealthIndicator method health.
@Override
public Health health() {
RateLimiter.Metrics metrics = rateLimiter.getMetrics();
int availablePermissions = metrics.getAvailablePermissions();
int numberOfWaitingThreads = metrics.getNumberOfWaitingThreads();
if (availablePermissions > 0 || numberOfWaitingThreads == 0) {
return rateLimiterHealth(Status.UP, availablePermissions, numberOfWaitingThreads);
}
if (rateLimiter instanceof AtomicRateLimiter) {
AtomicRateLimiter atomicRateLimiter = (AtomicRateLimiter) this.rateLimiter;
AtomicRateLimiter.AtomicRateLimiterMetrics detailedMetrics = atomicRateLimiter.getDetailedMetrics();
if (detailedMetrics.getNanosToWait() > timeoutInNanos) {
return rateLimiterHealth(Status.DOWN, availablePermissions, numberOfWaitingThreads);
}
}
return rateLimiterHealth(Status.UNKNOWN, availablePermissions, numberOfWaitingThreads);
}
use of io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter in project resilience4j by resilience4j.
the class RateLimiterHealthIndicatorTest method health.
@Test
public void health() throws Exception {
// given
RateLimiterConfig config = mock(RateLimiterConfig.class);
AtomicRateLimiter.AtomicRateLimiterMetrics metrics = mock(AtomicRateLimiter.AtomicRateLimiterMetrics.class);
AtomicRateLimiter rateLimiter = mock(AtomicRateLimiter.class);
// when
when(rateLimiter.getRateLimiterConfig()).thenReturn(config);
when(rateLimiter.getMetrics()).thenReturn(metrics);
when(rateLimiter.getDetailedMetrics()).thenReturn(metrics);
when(config.getTimeoutDuration()).thenReturn(Duration.ofNanos(30L));
when(metrics.getAvailablePermissions()).thenReturn(5, -1, -2);
when(metrics.getNumberOfWaitingThreads()).thenReturn(0, 1, 2);
when(metrics.getNanosToWait()).thenReturn(20L, 40L);
// then
RateLimiterHealthIndicator healthIndicator = new RateLimiterHealthIndicator(rateLimiter);
Health health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.UP);
health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.UNKNOWN);
health = healthIndicator.health();
then(health.getStatus()).isEqualTo(Status.DOWN);
then(health.getDetails()).contains(entry("availablePermissions", -2), entry("numberOfWaitingThreads", 2));
}
Aggregations