es.moki.ratelimitj.core.limiter.request.RequestLimitRule Java Examples
The following examples show how to use
es.moki.ratelimitj.core.limiter.request.RequestLimitRule.
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 |
@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 #2
Source File: InMemoryRequestRateLimiterInternalTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@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 #3
Source File: AbstractReactiveRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@Test void shouldGeLimitSingleWindowReactive() { ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 5)); ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit); Flux<Boolean> geLimitLimitFlux = Flux .just("ip:127.0.1.2") .repeat(3) .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(); }
Example #4
Source File: AbstractReactiveRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@Test void shouldLimitSingleWindowReactive() { ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(10), 5)); ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit); Flux<Boolean> overLimitFlux = Flux .just("ip:127.0.1.5") .repeat(4) .flatMap(key -> { timeBandit.addUnixTimeMilliSeconds(100); return rateLimiter.overLimitWhenIncrementedReactive(key); }); overLimitFlux.toStream().forEach(result -> assertThat(result).isFalse()); assertThat(rateLimiter.overLimitWhenIncrementedReactive("ip:127.0.1.5").block()).isTrue(); }
Example #5
Source File: HazelcastRequestRateLimiterInternalTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@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 #6
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@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 #7
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@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 #8
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 6 votes |
@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 #9
Source File: RedisSlidingWindowReactiveRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@Test void shouldReloadMissingScript() { ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(60), 1)); ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, new SystemTimeSupplier()); rateLimiter.overLimitWhenIncrementedReactive(UUID.randomUUID().toString()).block(Duration.ofSeconds(5)); getRedisScriptingReactiveCommands().scriptFlush().block(); rateLimiter.overLimitWhenIncrementedReactive(UUID.randomUUID().toString()).block(Duration.ofSeconds(5)); }
Example #10
Source File: RedisSlidingWindowRequestRateLimiter.java From ratelimitj with Apache License 2.0 | 5 votes |
public RedisSlidingWindowRequestRateLimiter(RedisScriptingReactiveCommands<String, String> redisScriptingReactiveCommands, RedisKeyReactiveCommands<String, String> redisKeyCommands, Set<RequestLimitRule> rules, TimeSupplier timeSupplier) { requireNonNull(rules, "rules can not be null"); requireNonNull(timeSupplier, "time supplier can not be null"); requireNonNull(redisScriptingReactiveCommands, "redisScriptingReactiveCommands can not be null"); requireNonNull(redisKeyCommands, "redisKeyCommands can not be null"); this.redisScriptingReactiveCommands = redisScriptingReactiveCommands; this.redisKeyCommands = redisKeyCommands; scriptLoader = new RedisScriptLoader(redisScriptingReactiveCommands, "sliding-window-ratelimit.lua"); requestLimitRulesSupplier = new SerializedRequestLimitRulesSupplier(rules); this.timeSupplier = timeSupplier; }
Example #11
Source File: InMemoryRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #12
Source File: RedisRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #13
Source File: SerializedRequestLimitRulesSupplier.java From ratelimitj with Apache License 2.0 | 5 votes |
SerializedRequestLimitRulesSupplier(Set<RequestLimitRule> rules) { this.serializedRuleMap = RequestLimitRulesSupplier.buildRuleMap(rules) .entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, kv -> serialiser.encode(kv.getValue()) )); this.serializedDefaultRuleSet = serialiser.encode(RequestLimitRulesSupplier.buildDefaultRuleSet(rules)); }
Example #14
Source File: RedisRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #15
Source File: RedisRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #16
Source File: RequestLimitRuleJsonSerialiserTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@Test @DisplayName("should encode limit rule in JSON array") void shouldEncode() { ImmutableList<RequestLimitRule> rules = ImmutableList.of(RequestLimitRule.of(Duration.ofSeconds(10), 10L), RequestLimitRule.of(Duration.ofMinutes(1), 20L)); assertThat(serialiser.encode(rules)).isEqualTo("[[10,10,10],[60,20,60]]"); }
Example #17
Source File: HazelcastSlidingWindowRequestRateLimiter.java From ratelimitj with Apache License 2.0 | 5 votes |
public HazelcastSlidingWindowRequestRateLimiter(HazelcastInstance hz, Set<RequestLimitRule> rules, TimeSupplier timeSupplier) { requireNonNull(hz, "hazelcast can not be null"); requireNonNull(rules, "rules can not be null"); if (rules.isEmpty()) { throw new IllegalArgumentException("at least one rule must be provided"); } requireNonNull(rules, "time supplier can not be null"); this.hz = hz; this.rulesSupplier = new DefaultRequestLimitRulesSupplier(rules); this.timeSupplier = timeSupplier; }
Example #18
Source File: InMemoryRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #19
Source File: InMemoryRateLimiterFactoryTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #20
Source File: RedisRateLimiterConfiguration.java From daming with Apache License 2.0 | 5 votes |
@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 #21
Source File: RequestLimitRuleJsonSerialiserTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@Test @DisplayName("should encode limit rule with precision in JSON array") void shouldEncodeWithPrecisions() { ImmutableList<RequestLimitRule> rules = ImmutableList.of(RequestLimitRule.of(Duration.ofSeconds(10), 10L).withPrecision(Duration.ofSeconds(4)), RequestLimitRule.of(Duration.ofMinutes(1), 20L).withPrecision(Duration.ofSeconds(8))); assertThat(serialiser.encode(rules)).isEqualTo("[[10,10,4],[60,20,8]]"); }
Example #22
Source File: InMemorySlidingWindowRequestRateLimiter.java From ratelimitj with Apache License 2.0 | 5 votes |
InMemorySlidingWindowRequestRateLimiter(ExpiringMap<String, ConcurrentMap<String, Long>> expiringKeyMap, Set<RequestLimitRule> rules, TimeSupplier timeSupplier) { requireNonNull(rules, "rules can not be null"); requireNonNull(rules, "time supplier can not be null"); if (rules.isEmpty()) { throw new IllegalArgumentException("at least one rule must be provided"); } this.expiringKeyMap = expiringKeyMap; this.timeSupplier = timeSupplier; this.rulesSupplier = new DefaultRequestLimitRulesSupplier(rules); }
Example #23
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #24
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #25
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #26
Source File: AbstractSyncRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@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 #27
Source File: RateLimiters.java From rebuild with GNU General Public License v3.0 | 5 votes |
/** * @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 #28
Source File: AbstractReactiveRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@Test void shouldLimitDualWindowReactive() { ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(1), 5), RequestLimitRule.of(Duration.ofSeconds(10), 10)); ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit); Flux .just("ip:127.0.1.6") .repeat(4) .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(4) .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(); }
Example #29
Source File: AbstractReactiveRequestRateLimiterTest.java From ratelimitj with Apache License 2.0 | 5 votes |
@Test void shouldResetLimit() { ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(Duration.ofSeconds(60), 1)); ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit); String key = "ip:127.1.0.1"; assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isFalse(); assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isTrue(); assertThat(rateLimiter.resetLimitReactive(key).block()).isTrue(); assertThat(rateLimiter.resetLimitReactive(key).block()).isFalse(); assertThat(rateLimiter.overLimitWhenIncrementedReactive(key).block()).isFalse(); }
Example #30
Source File: RedisSlidingWindowRequestRateLimiter.java From ratelimitj with Apache License 2.0 | 4 votes |
public RedisSlidingWindowRequestRateLimiter(RedisScriptingReactiveCommands<String, String> redisScriptingReactiveCommands, RedisKeyReactiveCommands<String, String> redisKeyCommands, Set<RequestLimitRule> rules) { this(redisScriptingReactiveCommands, redisKeyCommands, rules, new SystemTimeSupplier()); }