本文整理汇总了Java中org.isomorphism.util.TokenBuckets类的典型用法代码示例。如果您正苦于以下问题:Java TokenBuckets类的具体用法?Java TokenBuckets怎么用?Java TokenBuckets使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TokenBuckets类属于org.isomorphism.util包,在下文中一共展示了TokenBuckets类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: apply
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
public GatewayFilter apply(int capacity, int refillTokens, int refillPeriod, TimeUnit refillUnit) {
final TokenBucket tokenBucket = TokenBuckets.builder()
.withCapacity(capacity)
.withFixedIntervalRefillStrategy(refillTokens, refillPeriod, refillUnit)
.build();
return (exchange, chain) -> {
//TODO: get a token bucket for a key
log.debug("TokenBucket capacity: " + tokenBucket.getCapacity());
boolean consumed = tokenBucket.tryConsume();
if (consumed) {
return chain.filter(exchange);
}
exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
return exchange.getResponse().setComplete();
};
}
开发者ID:spring-cloud,项目名称:spring-cloud-gateway,代码行数:19,代码来源:ThrottleGatewayFilterFactory.java
示例2: createBucket
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
private static TokenBucket createBucket(@Nonnegative final long bits) {
// Instead of refilling once per second, we refill 10 times per second.
// It makes for a smoother bandwidth curve under very low rates,
// e.g., less than 100 kbps.
final long bytes = bits / Byte.SIZE;
final long refillRate = bytes / REFILL_FACTOR;
// if the division caused an underflow, revert to base units.
if (refillRate < 1) {
return TokenBuckets.builder()
.withCapacity(bytes)
.withYieldingSleepStrategy()
.withFixedIntervalRefillStrategy(bytes, BASE_INTERVAL, BASE_UNIT)
.build();
}
return TokenBuckets.builder()
.withCapacity(bytes)
.withYieldingSleepStrategy()
.withFixedIntervalRefillStrategy(refillRate, REFILL_INTERVAL, REFILL_UNIT)
.build();
}
开发者ID:ricardopadilha,项目名称:dsys-snio,代码行数:21,代码来源:TokenBucketLimiter.java
示例3: setIncreasedMessageBucket
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
/**
* Increased Token Bucket, when <b>isKnownBot</b> is <b>true</b>
* @return increased token bucket
*/
private TokenBucket setIncreasedMessageBucket() {
return TokenBuckets.builder()
.withCapacity(50)
.withFixedIntervalRefillStrategy(1, 600, TimeUnit.MILLISECONDS)
.build();
}
开发者ID:twitch4j,项目名称:twitch4j,代码行数:11,代码来源:TwitchChat.java
示例4: setDefaultMessageBucket
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
/**
* Default Token Bucket, when <b>isKnownBot</b> is <b>false</b>
* @return default token bucket
*/
private TokenBucket setDefaultMessageBucket() {
return TokenBuckets.builder()
.withCapacity(20)
.withFixedIntervalRefillStrategy(1, 1500, TimeUnit.MILLISECONDS)
.build();
}
开发者ID:twitch4j,项目名称:twitch4j,代码行数:11,代码来源:TwitchChat.java
示例5: joinChannel
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
/**
* Joining the channel
* @param channel channel name
*/
public void joinChannel(String channel) {
if (!channelCache.containsKey(channel)) {
sendCommand("join", "#" + channel);
channelCache.put(channel, new ChannelCache(this, channel));
Logger.debug(this, "Joining Channel [%s].", channel);
TokenBucket modBucket = TokenBuckets.builder()
.withCapacity(100)
.withFixedIntervalRefillStrategy(1, 300, TimeUnit.MILLISECONDS)
.build();
modMessageBucket.put(channel, modBucket);
}
}
开发者ID:twitch4j,项目名称:twitch4j,代码行数:19,代码来源:TwitchChat.java
示例6: updateBuckets
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
public void updateBuckets() {
int oldNbWorkers = nbWorkers;
nbWorkers = discoveryClient.getInstances(ServiceIds.worker()).size();
if (oldNbWorkers != nbWorkers) {
if (politeness.getPageMeanRate() > 0) {
pagePageRateBucket = TokenBuckets
.builder()
.withCapacity(
politeness.getPagePeekRate() > 0 && politeness.getPagePeekRate() > politeness.getPageMeanRate() ? politeness.getPagePeekRate()
/ nbWorkers : politeness.getPageMeanRate() / nbWorkers)
.withFixedIntervalRefillStrategy(politeness.getPageMeanRate() / nbWorkers, 1, TimeUnit.SECONDS).build();
} else {
pagePageRateBucket = null;
}
if (politeness.getPageMeanRate() > 0) {
bandwidthBucket = TokenBuckets
.builder()
.withCapacity(
politeness.getMaxPeekBandwith() > 0 && politeness.getMaxPeekBandwith() > politeness.getMaxBandwith() ? politeness
.getMaxPeekBandwith() / nbWorkers : politeness.getMaxBandwith() / nbWorkers)
.withFixedIntervalRefillStrategy(politeness.getPageMeanRate() / nbWorkers, 1, TimeUnit.SECONDS).build();
} else {
bandwidthBucket = null;
}
}
}
开发者ID:Treydone,项目名称:mandrel,代码行数:29,代码来源:Barrier.java
示例7: checkRateLimitAvailable
import org.isomorphism.util.TokenBuckets; //导入依赖的package包/类
private void checkRateLimitAvailable(Request request, Response response) throws ExecutionException {
TokenBucket tokenBucket = rateLimiters.get(currentUsername(), () -> TokenBuckets.builder()
.withCapacity(requestsPerMinute)
.withInitialTokens(requestsPerMinute)
.withRefillStrategy(new FixedIntervalRefillStrategy(ticker, requestsPerMinute, 1, TimeUnit.MINUTES))
.build());
response.header("X-RateLimit-Limit", String.valueOf(requestsPerMinute));
response.header("X-RateLimit-Remaining", String.valueOf(tokenBucket.getNumTokens()));
if (!tokenBucket.tryConsume()) {
throw HaltApiResponses.haltBecauseRateLimitExceeded();
}
}
开发者ID:gocd,项目名称:gocd,代码行数:15,代码来源:EncryptionControllerDelegate.java
注:本文中的org.isomorphism.util.TokenBuckets类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论