Java Code Examples for es.moki.ratelimitj.core.limiter.request.RequestLimitRule#of()

The following examples show how to use es.moki.ratelimitj.core.limiter.request.RequestLimitRule#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
void shouldLimitSingleWindowSyncWithKeySpecificRules() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofSeconds(10), 5).matchingKeys("ip:127.9.0.0");
    RequestLimitRule rule2 = RequestLimitRule.of(Duration.ofSeconds(10), 10);

    RequestRateLimiter requestRateLimiter = getRateLimiter(ImmutableSet.of(rule1, rule2), timeBandit);

    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(1000L);
        assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.9.0.0")).isFalse();
    });
    assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.9.0.0")).isTrue();

    IntStream.rangeClosed(1, 10).forEach(value -> assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.9.1.0")).isFalse());
    assertThat(requestRateLimiter.overLimitWhenIncremented("ip:127.9.1.0")).isTrue();
}
 
Example 2
Source File: InMemoryRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnTheSameInstanceForSameRules() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));

    RequestLimitRule rule2 = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));

    assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
 
Example 3
Source File: InMemoryRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnTheSameInstanceForSameSetOfRules() {

    RequestLimitRule rule1a = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestLimitRule rule1b = RequestLimitRule.of(Duration.ofHours(1), 100);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1a, rule1b));

    RequestLimitRule rule2a = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestLimitRule rule2b = RequestLimitRule.of(Duration.ofHours(1), 100);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2a, rule2b));

    assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
 
Example 4
Source File: InMemoryRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldNotReturnTheSameInstanceForSameRules() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofMinutes(1), 22);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));

    RequestLimitRule rule2 = RequestLimitRule.of(Duration.ofMinutes(1), 33);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));

    assertThat(rateLimiter1).isNotSameAs(rateLimiter2);
}
 
Example 5
Source File: RedisRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnTheSameInstanceForSameRules() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));

    RequestLimitRule rule2 = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));

    assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
 
Example 6
Source File: RedisRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReturnTheSameInstanceForSameSetOfRules() {

    RequestLimitRule rule1a = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestLimitRule rule1b = RequestLimitRule.of(Duration.ofHours(1), 100);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1a, rule1b));

    RequestLimitRule rule2a = RequestLimitRule.of(Duration.ofMinutes(1), 10);
    RequestLimitRule rule2b = RequestLimitRule.of(Duration.ofHours(1), 100);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2a, rule2b));

    assertThat(rateLimiter1).isSameAs(rateLimiter2);
}
 
Example 7
Source File: RedisRateLimiterFactoryTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldNotReturnTheSameInstanceForSameRules() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofMinutes(1), 22);
    RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));

    RequestLimitRule rule2 = RequestLimitRule.of(Duration.ofMinutes(1), 33);
    RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));

    assertThat(rateLimiter1).isNotSameAs(rateLimiter2);
}
 
Example 8
Source File: RateLimit429EnforcerFilter.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
private RequestLimitRule toLimitRule(Rate rate) {
    return RequestLimitRule.of(Duration.ofMillis(rate.timeUnit().toMillis(rate.duration())), rate.limit());
}