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); }
private boolean eqOrGeLimit(String key, int weight, boolean strictlyGreater) { final Set<RequestLimitRule> rules = rulesSupplier.getRules(key);
private boolean eqOrGeLimit(String key, int weight, boolean strictlyGreater) { final Set<RequestLimitRule> rules = rulesSupplier.getRules(key);
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; }
DefaultRequestLimitRulesSupplierTest() { allRules.add(RequestLimitRule.of(Duration.ofSeconds(1), 10).withName("localhostPerSeconds") .matchingKeys("localhost", "127.0.0.1")); allRules.add(RequestLimitRule.of(Duration.ofHours(1), 2000).withName("localhostPerHours") .matchingKeys("localhost", "127.0.0.1")); allRules.add(RequestLimitRule.of(Duration.ofSeconds(1), 5).withName("perSeconds")); allRules.add(RequestLimitRule.of(Duration.ofHours(1), 1000).withName("perHours")); requestLimitRulesSupplier = new DefaultRequestLimitRulesSupplier(allRules); }