Search in sources :

Example 1 with RequestRateLimiter

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

the class InMemoryRateLimiterFactoryTest method shouldReturnTheSameInstanceForSameSetOfRules.

@Test
void shouldReturnTheSameInstanceForSameSetOfRules() {
    RequestLimitRule rule1a = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestLimitRule rule1b = RequestLimitRule.of(1, TimeUnit.HOURS, 100);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1a, rule1b));
    RequestLimitRule rule2a = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestLimitRule rule2b = RequestLimitRule.of(1, TimeUnit.HOURS, 100);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2a, rule2b));
    assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
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 2 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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 3 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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 4 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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 5 with RequestRateLimiter

use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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)

Aggregations

RequestRateLimiter (es.moki.ratelimitj.core.limiter.request.RequestRateLimiter)15 RequestLimitRule (es.moki.ratelimitj.core.limiter.request.RequestLimitRule)14 Test (org.junit.jupiter.api.Test)14 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DistributedObject (com.hazelcast.core.DistributedObject)1 RateLimited (es.moki.ratelimij.dropwizard.annotation.RateLimited)1 TimeSupplier (es.moki.ratelimitj.core.time.TimeSupplier)1 TimeBanditSupplier (es.moki.ratelimitj.test.time.TimeBanditSupplier)1 NumberFormat (java.text.NumberFormat)1 Locale (java.util.Locale)1 Random (java.util.Random)1 Set (java.util.Set)1 TimeUnit (java.util.concurrent.TimeUnit)1 IntStream (java.util.stream.IntStream)1 AnnotatedMethod (org.glassfish.jersey.server.model.AnnotatedMethod)1 Disabled (org.junit.jupiter.api.Disabled)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1