Search in sources :

Example 11 with RequestLimitRule

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

the class RedisRateLimiterFactoryTest method shouldReturnTheSameInstanceForSameRules.

@Test
void shouldReturnTheSameInstanceForSameRules() {
    RequestLimitRule rule1 = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));
    RequestLimitRule rule2 = RequestLimitRule.of(1, TimeUnit.MINUTES, 10);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));
    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 12 with RequestLimitRule

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

the class AbstractAsyncRequestRateLimiterTest method shouldResetLimit.

@Test
void shouldResetLimit() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
    AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
    String key = "ip:127.1.0.1";
    assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
    assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isTrue();
    assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isTrue();
    assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isFalse();
    assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
}
Also used : AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 13 with RequestLimitRule

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

the class AbstractAsyncRequestRateLimiterTest method shouldLimitDualWindowAsync.

@Test
void shouldLimitDualWindowAsync() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 20));
    AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
    Queue<CompletionStage> stageAsserts = new ConcurrentLinkedQueue<>();
    Stream.generate(() -> "ip:127.0.0.10").limit(5).forEach(key -> {
        timeBandit.addUnixTimeMilliSeconds(200L);
        log.debug("incrementing rate limiter");
        stageAsserts.add(rateLimiter.overLimitAsync(key).thenAccept(result -> assertThat(result).isFalse()));
    });
    for (CompletionStage stage : stageAsserts) {
        stage.toCompletableFuture().get();
    }
    assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isTrue();
    timeBandit.addUnixTimeMilliSeconds(1000L);
    assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isFalse();
}
Also used : AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) TimeSupplier(es.moki.ratelimitj.core.time.TimeSupplier) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Queue(java.util.Queue) TimeBanditSupplier(es.moki.ratelimitj.test.time.TimeBanditSupplier) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AsyncRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.AsyncRequestRateLimiter) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.jupiter.api.Test)

Example 14 with RequestLimitRule

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

the class AbstractReactiveRequestRateLimiterTest method shouldLimitDualWindowReactive.

@Test
void shouldLimitDualWindowReactive() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(1, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 10));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    Flux.just("ip:127.0.1.6").repeat(5).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.overLimitWhenIncrementedReactive(key);
    }).toStream().forEach(result -> assertThat(result).isFalse());
    timeBandit.addUnixTimeMilliSeconds(1000L);
    Flux.just("ip:127.0.1.6").repeat(5).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.overLimitWhenIncrementedReactive(key);
    }).toStream().forEach(result -> assertThat(result).isFalse());
    assertThat(rateLimiter.overLimitWhenIncrementedReactive("ip:127.0.1.6").block()).isTrue();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) RequestLimitRule(es.moki.ratelimitj.core.limiter.request.RequestLimitRule) Test(org.junit.jupiter.api.Test)

Example 15 with RequestLimitRule

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

the class AbstractReactiveRequestRateLimiterTest method shouldGeLimitSingleWindowReactive.

@Test
void shouldGeLimitSingleWindowReactive() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
    ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
    Flux<Boolean> geLimitLimitFlux = Flux.just("ip:127.0.1.2").repeat(4).flatMap(key -> {
        timeBandit.addUnixTimeMilliSeconds(100);
        return rateLimiter.geLimitWhenIncrementedReactive(key);
    });
    geLimitLimitFlux.toStream().forEach(result -> assertThat(result).isFalse());
    assertThat(rateLimiter.geLimitWhenIncrementedReactive("ip:127.0.1.2").block()).isTrue();
}
Also used : ReactiveRequestRateLimiter(es.moki.ratelimitj.core.limiter.request.ReactiveRequestRateLimiter) 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