Search in sources :

Example 1 with AtomicRateLimiter

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);
}
Also used : SemaphoreBasedRateLimiter(io.github.resilience4j.ratelimiter.internal.SemaphoreBasedRateLimiter) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with AtomicRateLimiter

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);
}
Also used : RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)

Example 3 with AtomicRateLimiter

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));
}
Also used : Health(org.springframework.boot.actuate.health.Health) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) AtomicRateLimiter(io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter) Test(org.junit.Test)

Aggregations

AtomicRateLimiter (io.github.resilience4j.ratelimiter.internal.AtomicRateLimiter)3 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)1 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)1 SemaphoreBasedRateLimiter (io.github.resilience4j.ratelimiter.internal.SemaphoreBasedRateLimiter)1 Test (org.junit.Test)1 Setup (org.openjdk.jmh.annotations.Setup)1 Health (org.springframework.boot.actuate.health.Health)1