本文整理汇总了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();
}