es.moki.ratelimitj.core.limiter.request.RequestRateLimiter Java Examples

The following examples show how to use es.moki.ratelimitj.core.limiter.request.RequestRateLimiter. 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: HazelcastRequestRateLimiterInternalTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(2), 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);

    String key = "ip:127.0.0.5";

    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(100L);
        assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
    });

    IMap<Object, Object> map = hz.getMap(key);
    while (map.size() != 0) {
        Thread.sleep(10);
    }
    assertThat(map.size()).isZero();
}
 
Example #2
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
void shouldLimitSingleWindowSyncWithMultipleKeys() {

    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 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());
}
 
Example #3
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 #4
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
void shouldRateLimitOverTime() {
    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofSeconds(5), 250).withPrecision(Duration.ofSeconds(1)).matchingKeys("ip:127.3.9.3");
    RequestRateLimiter requestRateLimiter = getRateLimiter(ImmutableSet.of(rule1), timeBandit);
    AtomicLong timeOfLastOperation = new AtomicLong();

    IntStream.rangeClosed(1, 50).forEach(loop -> {

        IntStream.rangeClosed(1, 250).forEach(value -> {
            timeBandit.addUnixTimeMilliSeconds(14L);
            boolean overLimit = requestRateLimiter.overLimitWhenIncremented("ip:127.3.9.3");
            if (overLimit) {
                long timeSinceLastOperation = timeBandit.get() - timeOfLastOperation.get();
                assertThat(timeSinceLastOperation).isLessThan(3);
            } else {
                timeOfLastOperation.set(timeBandit.get());
            }
        });

    });
}
 
Example #5
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test @Disabled
void shouldPreventThunderingHerdWithPrecision() {

    RequestLimitRule rule1 = RequestLimitRule.of(Duration.ofSeconds(5), 250).withPrecision(Duration.ofSeconds(1)).matchingKeys("ip:127.9.9.9");
    RequestRateLimiter requestRateLimiter = getRateLimiter(ImmutableSet.of(rule1), timeBandit);
    Map<Long, Integer> underPerSecond = new LinkedHashMap<>();
    Map<Long, Integer> overPerSecond = new HashMap<>();

    IntStream.rangeClosed(1, 50).forEach(loop -> {

        IntStream.rangeClosed(1, 250).forEach(value -> {
            timeBandit.addUnixTimeMilliSeconds(14L);
            boolean overLimit = requestRateLimiter.overLimitWhenIncremented("ip:127.9.9.9");
            if (!overLimit) {
                underPerSecond.merge(timeBandit.get(), 1, Integer::sum);
            } else {
                overPerSecond.merge(timeBandit.get(), 1, Integer::sum);
            }
        });

    });

    Set<Long> allSeconds = Sets.newTreeSet(Sets.union(underPerSecond.keySet(), overPerSecond.keySet()));

    allSeconds.forEach((k)->System.out.println("Time seconds : " + k + " under count : " + underPerSecond.get(k) + " over count : " + overPerSecond.get(k)));
}
 
Example #6
Source File: InMemoryRequestRateLimiterInternalTest.java    From ratelimitj with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(1), 5));
    RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);

    String key = "ip:127.0.0.5";

    IntStream.rangeClosed(1, 5).forEach(value -> {
        timeBandit.addUnixTimeMilliSeconds(100L);
        assertThat(requestRateLimiter.overLimitWhenIncremented(key)).isFalse();
    });

    while (expiryingKeyMap.size() != 0) {
        Thread.sleep(50);
    }

    assertThat(expiryingKeyMap.size()).isZero();
}
 
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: RedisRateLimiterConfiguration.java    From daming with Apache License 2.0 5 votes vote down vote up
@Bean
public RedisSlidingWindowByMobileRateLimiter redisSlidingWindowByMobileRateLimiter(
        RequestRateLimiterFactory redisRateLimiterFactory, SlidingWindowProperties slidingWindowProperties) {
    Set<RequestLimitRule> rules = Collections.singleton(
            RequestLimitRule.of(slidingWindowProperties.getDuration(), slidingWindowProperties.getLimit()));
    RequestRateLimiter requestRateLimiter = redisRateLimiterFactory.getInstance(rules);
    return new RedisSlidingWindowByMobileRateLimiter(requestRateLimiter);
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: RateLimiters.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param seconds
 * @param limit
 * @return
 */
public static RequestRateLimiter createRateLimiter(long[] seconds, int[] limit) {
    Assert.isTrue(seconds.length == limit.length, "Rule pair not matchs");

    Set<RequestLimitRule> rules = new HashSet<>();
    for (int i = 0; i < seconds.length; i++) {
        rules.add(RequestLimitRule.of(Duration.ofSeconds(seconds[i]), limit[i]));
    }
    return new InMemorySlidingWindowRequestRateLimiter(rules);
}
 
Example #15
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldResetLimit() {
    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(60), 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();
}
 
Example #16
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldLimitWithWeightSingleWindowSync() {

    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 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();
}
 
Example #17
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldGeLimitSingleWindowSync() {

    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 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();
}
 
Example #18
Source File: AbstractSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Test
void shouldLimitSingleWindowSync()  {

    ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 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();
}
 
Example #19
Source File: InMemorySlidingWindowSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new InMemorySlidingWindowRequestRateLimiter(rules, timeSupplier);
}
 
Example #20
Source File: RedisSlidingWindowSyncRequestRequestRateLimiterPerformanceTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new RedisSlidingWindowRequestRateLimiter(extension.getScriptingReactiveCommands(), extension.getKeyReactiveCommands(), rules, timeSupplier);
}
 
Example #21
Source File: RedisSlidingWindowSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new RedisSlidingWindowRequestRateLimiter(extension.getScriptingReactiveCommands(), extension.getKeyReactiveCommands(), rules, timeSupplier);
}
 
Example #22
Source File: RedisRateLimiterFactory.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
public RequestRateLimiter getInstance(Set<RequestLimitRule> rules) {
    return lookupInstance(rules);
}
 
Example #23
Source File: HazelcastSlidingWindowSyncRequestRateLimiterTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new HazelcastSlidingWindowRequestRateLimiter(hz, rules, timeSupplier);
}
 
Example #24
Source File: HazelcastRequestRateLimiterInternalTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
private RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new HazelcastSlidingWindowRequestRateLimiter(hz, rules, timeSupplier);
}
 
Example #25
Source File: HazelcastSlidingWindowSyncRequestRequestRateLimiterPerformanceTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new HazelcastSlidingWindowRequestRateLimiter(hz, rules, timeSupplier);
}
 
Example #26
Source File: RateLimit429EnforcerFilter.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(final ContainerRequestContext requestContext) {

    try {
        AnnotatedMethod method = new AnnotatedMethod(resource.getResourceMethod());
        RateLimited rateLimited = method.getAnnotation(RateLimited.class);

        RequestRateLimiter rateLimit = factory.getInstance(toLimitRules(rateLimited));

        KeyPart[] keyParts = rateLimited.keys();


        Optional<CharSequence> keyResult = KeyPart.combineKeysParts(rateLimited.groupKeyPrefix(), Arrays.asList(keyParts), request, resource, securityContext);


        CharSequence key;
        if (keyResult.isPresent()) {
            key = keyResult.get();
        } else {
            LOG.warn("No keys were provided by the key providers '{}'",
                    Arrays.stream(keyParts)
                            .map(KeyPart::getClass)
                            .map(Object::toString)
                            .collect(Collectors.joining(", ")));
            return;
        }

        boolean overLimit = rateLimit.overLimitWhenIncremented(key.toString());
        if (overLimit) {
            if (!rateLimited.reportOnly()) {
                LOG.info("rate-limit key '{}' over limit. HTTP Status 429 returned.", key);
                requestContext.abortWith(Response.status(HTTP_STATUS_TOO_MANY_REQUESTS).build());
            } else {
                LOG.info("rate-limit key '{}' over limit. ReportOnly is true, no action taken.", key);
            }
            LOG.debug("rate-limit key '{}' under limit.", key);
        }
    } catch (Exception e) {
        LOG.error("Error occurred checking rate-limit. Assuming under limit", e);
    }
}
 
Example #27
Source File: InMemorySlidingWindowSyncRequestRequestRateLimiterPerformanceTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
protected RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new InMemorySlidingWindowRequestRateLimiter(rules, timeSupplier);
}
 
Example #28
Source File: InMemoryRequestRateLimiterInternalTest.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
private RequestRateLimiter getRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
    return new InMemorySlidingWindowRequestRateLimiter(expiryingKeyMap, rules, timeSupplier);
}
 
Example #29
Source File: InMemoryRateLimiterFactory.java    From ratelimitj with Apache License 2.0 4 votes vote down vote up
@Override
public RequestRateLimiter getInstance(Set<RequestLimitRule> rules) {
    requireNonNull(rules);
    return lookupInstance(rules);
}
 
Example #30
Source File: RateLimiters.java    From rebuild with GNU General Public License v3.0 2 votes vote down vote up
/**
 * @param seconds
 * @param limit
 * @return
 */
public static RequestRateLimiter createRateLimiter(long seconds, int limit) {
    Set<RequestLimitRule> rules = Collections.singleton(RequestLimitRule.of(Duration.ofSeconds(seconds), limit));
    return new InMemorySlidingWindowRequestRateLimiter(rules);
}