public DefaultRequestLimitRulesSupplier(Set<RequestLimitRule> rules) { this.defaultRules = RequestLimitRulesSupplier.buildDefaultRuleSet(rules); this.ruleMap = RequestLimitRulesSupplier.buildRuleMap(rules); }
@Test void shouldContainLocalhostRulesForLocalhost() { Set<String> ruleNames = requestLimitRulesSupplier.getRules("localhost") .stream() .map(RequestLimitRule::getName) .collect(Collectors.toSet()); assertThat(ruleNames).hasSize(2); assertThat(ruleNames).contains("localhostPerSeconds", "localhostPerHours"); }
@Test void shouldContainLocalhostRulesFor127_0_0_1() { Set<String> ruleNames = requestLimitRulesSupplier.getRules("127.0.0.1") .stream() .map(RequestLimitRule::getName) .collect(Collectors.toSet()); assertThat(ruleNames).hasSize(2); assertThat(ruleNames).contains("localhostPerSeconds", "localhostPerHours"); }
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)); }
@Test void shouldContainsDefaultRules() { Set<String> ruleNames = requestLimitRulesSupplier.getRules("other") .stream() .map(RequestLimitRule::getName) .collect(Collectors.toSet()); assertThat(ruleNames).hasSize(2); assertThat(ruleNames).contains("perSeconds", "perHours"); }
private Mono<Boolean> eqOrGeLimitReactive(String key, int weight, boolean strictlyGreater) { requireNonNull(key); String rulesJson = requestLimitRulesSupplier.getRules(key); return Mono.zip(timeSupplier.getReactive(), scriptLoader.storedScript()) .flatMapMany(tuple -> { Long time = tuple.getT1(); StoredScript script = tuple.getT2(); return redisScriptingReactiveCommands .evalsha(script.getSha(), VALUE, new String[]{key}, rulesJson, time.toString(), Integer.toString(weight), toStringOneZero(strictlyGreater)) .doOnError(STARTS_WITH_NO_SCRIPT_ERROR, e -> script.dispose()); }) .retry(1, STARTS_WITH_NO_SCRIPT_ERROR) .single() .map("1"::equals) .doOnSuccess(over -> { if (over) { LOG.debug("Requests matched by key '{}' incremented by weight {} are greater than {}the limit", key, weight, strictlyGreater ? "" : "or equal to "); } }); }