use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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();
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter 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);
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class RateLimit429EnforcerFilter method filter.
@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));
KeyProvider keyProvider = rateLimited.key();
KeyPart[] keyParts = rateLimited.keys();
if (keyProvider == Key.NO_VALUE && keyParts.length == 0) {
LOG.warn("No keys were provided by the key provide");
return;
}
Optional<CharSequence> legacyKey = keyProvider.create(request, resource, securityContext);
CharSequence key;
if (legacyKey.isPresent()) {
key = legacyKey.get();
} else {
Optional<CharSequence> keyResult = KeyPart.combineKeysParts(rateLimited.groupKeyPrefix(), Arrays.asList(keyParts), request, resource, securityContext);
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;
}
}
// if (legacyKey.isPresent()) {
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);
}
// } else {
// //LOG.warn("No key was provided by the key provide '{}'", keyProvider.getClass());
// }
} catch (Exception e) {
LOG.error("Error occurred checking rate-limit. Assuming under limit", e);
}
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class AbstractSyncRequestRateLimiterPerformanceTest method shouldLimitDualWindowSyncTimed.
@Test
void shouldLimitDualWindowSyncTimed() {
Stopwatch watch = Stopwatch.createStarted();
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 100), RequestLimitRule.of(10, TimeUnit.SECONDS, 100));
RequestRateLimiter requestRateLimiter = getRateLimiter(rules, timeBandit);
Random rand = new Random();
int total = 10_000;
IntStream.rangeClosed(1, total).map(i -> rand.nextInt(128)).forEach(value -> {
timeBandit.addUnixTimeMilliSeconds(200L);
requestRateLimiter.overLimitWhenIncremented("ip:127.0.0." + value);
});
double transactionsPerSecond = Math.ceil((double) total / watch.elapsed(TimeUnit.MILLISECONDS) * 1000);
log.info("total time {} checks {}/sec", watch.stop(), NumberFormat.getNumberInstance(Locale.US).format(transactionsPerSecond));
}
use of es.moki.ratelimitj.core.limiter.request.RequestRateLimiter in project ratelimitj by mokies.
the class InMemoryRateLimiterFactoryTest method shouldNotReturnTheSameInstanceForSameRules.
@Test
void shouldNotReturnTheSameInstanceForSameRules() {
RequestLimitRule rule1 = RequestLimitRule.of(1, TimeUnit.MINUTES, 22);
RequestRateLimiter rateLimiter1 = factory.getInstance(ImmutableSet.of(rule1));
RequestLimitRule rule2 = RequestLimitRule.of(1, TimeUnit.MINUTES, 33);
RequestRateLimiter rateLimiter2 = factory.getInstance(ImmutableSet.of(rule2));
assertThat(rateLimiter1).isNotSameAs(rateLimiter2);
}
Aggregations