本文整理匯總了Java中es.moki.ratelimitj.core.limiter.request.RequestLimitRule類的典型用法代碼示例。如果您正苦於以下問題:Java RequestLimitRule類的具體用法?Java RequestLimitRule怎麽用?Java RequestLimitRule使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RequestLimitRule類屬於es.moki.ratelimitj.core.limiter.request包,在下文中一共展示了RequestLimitRule類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: shouldLimitSingleWindowSyncWithMultipleKeys
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitSingleWindowSyncWithMultipleKeys() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 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());
}
示例2: shouldLimitSingleWindowReactive
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitSingleWindowReactive() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
Flux<Boolean> overLimitFlux = Flux
.just("ip:127.0.1.5")
.repeat(5)
.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();
}
示例3: shouldGeLimitSingleWindowReactive
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldGeLimitSingleWindowReactive() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
Flux<Boolean> geLimitLimitFlux = Flux
.just("ip:127.0.1.2")
.repeat(4)
.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();
}
示例4: shouldLimitSingleWindowAsync
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitSingleWindowAsync() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 5));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
Queue<CompletionStage> stageAsserts = new ConcurrentLinkedQueue<>();
Stream.generate(() -> "ip:127.0.0.2").limit(5).forEach(key -> {
timeBandit.addUnixTimeMilliSeconds(1000L);
stageAsserts.add(rateLimiter.overLimitAsync(key)
.thenAccept(result -> assertThat(result).isFalse()));
});
for (CompletionStage stage : stageAsserts) {
stage.toCompletableFuture().get();
}
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.2").toCompletableFuture().get()).isTrue();
}
示例5: shouldLimitDualWindowAsync
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitDualWindowAsync() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 20));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
Queue<CompletionStage> stageAsserts = new ConcurrentLinkedQueue<>();
Stream.generate(() -> "ip:127.0.0.10").limit(5).forEach(key -> {
timeBandit.addUnixTimeMilliSeconds(200L);
log.debug("incrementing rate limiter");
stageAsserts.add(rateLimiter.overLimitAsync(key)
.thenAccept(result -> assertThat(result).isFalse()));
});
for (CompletionStage stage : stageAsserts) {
stage.toCompletableFuture().get();
}
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isTrue();
timeBandit.addUnixTimeMilliSeconds(1000L);
assertThat(rateLimiter.overLimitAsync("ip:127.0.0.10").toCompletableFuture().get()).isFalse();
}
示例6: shouldEventuallyCleanUpExpiredKeys
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
@Disabled
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(1, TimeUnit.SECONDS, 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();
}
示例7: shouldEventuallyCleanUpExpiredKeys
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldEventuallyCleanUpExpiredKeys() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(2, TimeUnit.SECONDS, 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();
}
示例8: shouldLimitSingleWindowSync
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitSingleWindowSync() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 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();
}
示例9: shouldGeLimitSingleWindowSync
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@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();
}
示例10: shouldLimitWithWeightSingleWindowSync
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitWithWeightSingleWindowSync() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(10, TimeUnit.SECONDS, 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();
}
示例11: shouldResetLimit
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldResetLimit() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 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();
}
示例12: shouldLimitDualWindowReactive
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldLimitDualWindowReactive() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(1, TimeUnit.SECONDS, 5), RequestLimitRule.of(10, TimeUnit.SECONDS, 10));
ReactiveRequestRateLimiter rateLimiter = getRateLimiter(rules, timeBandit);
Flux
.just("ip:127.0.1.6")
.repeat(5)
.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(5)
.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();
}
示例13: shouldResetLimit
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldResetLimit() {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 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();
}
示例14: shouldResetLimit
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
@Test
void shouldResetLimit() throws Exception {
ImmutableSet<RequestLimitRule> rules = ImmutableSet.of(RequestLimitRule.of(60, TimeUnit.SECONDS, 1));
AsyncRequestRateLimiter rateLimiter = getAsyncRateLimiter(rules, timeBandit);
String key = "ip:127.1.0.1";
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isTrue();
assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isTrue();
assertThat(rateLimiter.resetLimitAsync(key).toCompletableFuture().get()).isFalse();
assertThat(rateLimiter.overLimitAsync(key).toCompletableFuture().get()).isFalse();
}
示例15: InMemorySlidingWindowRequestRateLimiter
import es.moki.ratelimitj.core.limiter.request.RequestLimitRule; //導入依賴的package包/類
public InMemorySlidingWindowRequestRateLimiter(Set<RequestLimitRule> rules, TimeSupplier timeSupplier) {
requireNonNull(rules, "rules can not be null");
requireNonNull(rules, "time supplier can not be null");
this.rules = rules;
this.timeSupplier = timeSupplier;
this.expiringKeyMap = ExpiringMap.builder().variableExpiration().build();
}