public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
//尝试获取资源最多等待时间
long timeoutMicros = max(unit.toMicros(timeout), 0);
//检查获取资源数目是否正确
checkPermits(permits);
long microsToWait;
//加锁
synchronized (mutex()) {
//当前时间
long nowMicros = stopwatch.readMicros();
//判断是否可以在timeout时间内获取资源
if (!canAcquire(nowMicros, timeoutMicros)) {
return false;
} else {
//可获取资源,对资源进行重新计算,并返回当前线程需要休眠时间
microsToWait = reserveAndGetWaitLength(permits, nowMicros);
}
}
//休眠
stopwatch.sleepMicrosUninterruptibly(microsToWait);
return true;
}
private boolean canAcquire(long nowMicros, long timeoutMicros) {
//最早可获取资源时间-等待时间<=当前时间 方可获取资源
return queryEarliestAvailable(nowMicros) - timeoutMicros <= nowMicros;
}
final long reserveAndGetWaitLength(int permits, long nowMicros) {
//获取下次可获取时间
long momentAvailable = reserveEarliestAvailable(permits, nowMicros);
//计算当前线程需要休眠时间
return max(momentAvailable - nowMicros, 0);
}
final long reserveEarliestAvailable(int requiredPermits, long nowMicros) {
//重新计算桶内令牌数storedPermits
resync(nowMicros);
long returnValue = nextFreeTicketMicros;
//本次消耗的令牌数
double storedPermitsToSpend = min(requiredPermits, this.storedPermits);
//重新计算下次可获取时间nextFreeTicketMicros
double freshPermits = requiredPermits - storedPermitsToSpend;
long waitMicros =
storedPermitsToWaitTime(this.storedPermits, storedPermitsToSpend)
+ (long) (freshPermits * stableIntervalMicros);
this.nextFreeTicketMicros = LongMath.saturatedAdd(nextFreeTicketMicros, waitMicros);
//减少桶内令牌数
this.storedPermits -= storedPermitsToSpend;
return returnValue;
}
public SimpleRateLimitInterceptor(int rate) {
if (rate > 0)
globalRateLimiter = RateLimiter.create(rate);
else
throw new RuntimeException("rate must greater than zero");
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!globalRateLimiter.tryAcquire()) {
LoggerUtil.log(request.getRequestURI()+"请求超过限流器速率");
return false;
}
return true;
}
<mvc:interceptors>
<!--限流拦截器-->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="limit.SimpleRateLimitInterceptor">
<constructor-arg index="0" value="${totalRate}"/>
</bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="limit.RateLimitInterceptor">
<!--单url限流-->
<property name="urlProperties">
<props>
<prop key="/get/{id}">1</prop>
<prop key="/post">2</prop>
</props>
</property>
</bean>
</mvc:interceptor>
//当前请求路径
String lookupPath = urlPathHelper.getLookupPathForRequest(request);
//迭代所有url表达式对应的PatternsRequestCondition
for (PatternsRequestCondition patternsRequestCondition : urlRateMap.keySet()) {
//进行匹配
List<String> matches = patternsRequestCondition.getMatchingPatterns(lookupPath);
if (!matches.isEmpty()) {
//匹配成功的则获取对应限流器的令牌
if (urlRateMap.get(patternsRequestCondition).tryAcquire()) {
LoggerUtil.log(lookupPath + " 请求匹配到" + Joiner.on(",").join(patternsRequestCondition.getPatterns()) + "限流器");
} else {
//获取令牌失败
LoggerUtil.log(lookupPath + " 请求超过" + Joiner.on(",").join(patternsRequestCondition.getPatterns()) + "限流器速率");
return false;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有