Search in sources :

Example 6 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class AbstractSyncRequestRateLimiterTest method shouldLimitSingleWindowSync.

@Test
void shouldLimitSingleWindowSync() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(1000L);
        assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.1")).isFalse();
    });
    assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.1")).isTrue();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 7 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class AbstractSyncRequestRateLimiterTest method shouldLimitSingleWindowSyncWithMultipleKeys.

@Test
void shouldLimitSingleWindowSyncWithMultipleKeys() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(1000L);
        IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isFalse());
    });
    IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isTrue());
    timeBandit.addUnixTimeMilliSeconds(5000L);
    IntStream.rangeClosed(1, 10).forEach(keySuffix -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + keySuffix)).isFalse());
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 8 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class AbstractSyncRequestRateLimiterTest method shouldLimitWithWeightSingleWindowSync.

@Test
void shouldLimitWithWeightSingleWindowSync() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 10));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(1000L);
        assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.2", 2)).isFalse();
    });
    assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.0.1.2", 2)).isTrue();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 9 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class AbstractSyncRequestRateLimiterTest method shouldResetLimit.

@Test
void shouldResetLimit() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    String key = "ip:127.1.0.1";
    assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
    assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isTrue();
    assertThat(requestRateLimiter.resetLimit(key)).isTrue();
    assertThat(requestRateLimiter.resetLimit(key)).isFalse();
    assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 10 with RequestLimitRule

use of es.moki.ratelimitj.core.limiter.request.RequestLimitRule in project ratelimitj by mokies.

the class AbstractSyncRequestRateLimiterTest method shouldGeLimitSingleWindowSync.

@Test
void shouldGeLimitSingleWindowSync() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
    IntStream.rangeClosed(1, 4).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(1000L);
        assertThat(requestRateLimiter.geLimitWhenIncremented("ip:127.0.1.2")).isFalse();
    });
    assertThat(requestRateLimiter.geLimitWhenIncremented("ip:127.0.1.2")).isTrue();
}
Also used : RequestRateLimiter(es.moki.ratelimitj.core.limiter.request.RequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Aggregations

RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)23 Test (org.junit.jupiter.api.Test)21 RequestRateLimiter (es.moki.ratelimitj.core.limiter.request.RequestRateLimiter)16 TimeSupplier (es.moki.ratelimitj.core.time.TimeSupplier)5 Set (java.util.Set)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 ReactiveRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter)4 TimeUnit (java.util.concurrent.TimeUnit)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 AsyncRequestRateLimiter (es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter)3 TimeBanditSupplier (es.moki.ratelimitj.test.time.TimeBanditSupplier)3 RateLimitUtils.coalesce (es.moki.ratelimitj.core.RateLimitUtils.coalesce)2 SystemTimeSupplier (es.moki.ratelimitj.core.time.SystemTimeSupplier)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Queue (java.util.Queue)2 CompletionStage (java.util.concurrent.CompletionStage)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2