谈谈杜博的电影明星-betway体育_必威体育·betway亚洲入口

本文首要研究一下dubbo的TPSLimiter

TPSLimiter

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/TPSLimiter.java

public interface TPSLimiter {

/**
* judge if the current invocation is allowed by TPS rule
*
* @param url ur洗浴中心l
* @param invocation invocation
* @return true allow the current invocation, otherwise, return false
*/
boo苔lean isAllowable(URL url, Invocation invocation);

}
  • TPSLimiter界说了isAllowable办法

DefaultTPSLimiter

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/DefaultTPSLimiter.java

public class DefaultTPSLimiter implements TPSLimiter {

private final ConcurrentMap stats = new ConcurrentHashMap();

@Override
public boolean isAllowable(URL url, Invocation invocation) {
int rate = url.getParameter(TPS_LIMI警车T_RATE_KEY, -1);
long interval = url.getParamete谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口r(TPS_LIMIT_INTERVAL_KEY, DEFAULT_TPS_LIMIT_INTERVAL);
String serviceKey = url.getServiceKey();
if (rate > 0) {
StatItem statItem = stat少年包青天1s.get(serviceKey);
if (statItem == null) {
stats.putIfAbsent(s四级成果erviceKey, new StatItem(serviceKey, rat胃炎吃什么药e, interval));
statItem = stats.get(serviceKey);
} else {
//rate or interval has changed, rebuild
if (statItem.getR罗京ate() != rate || statItem.getInterval() != interval宁丹琳) {
stats.put(serviceKey, new StatItem(serviceKey, rate, interval));
statItem = stats.get(serviceKey);侧耳倾听
}
}
return statItem.isAllowable();
} else {
StatItem statItem = stats.get(serviceKey);
if (statItem != null)imp {
stats.remove(service单博丽Key);
}
}

return true;
}

}
  • DefaultTPSLimiter完成了TPSLimiter,它运用Concurre谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口ntHashMap来存储StatItem,其key为URL中的serviceKey;isAllowable办法从URL中读取tps参数,默以为-1,小于0则从ConcurrentHashMap中移除,大于0则创立或许获取StatItem,调用StatItem的isAllowable(重置或递减token并回来结铜雀台果)

StatItem

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/tps/StatItem.java

class StatItem {

private String name;

private long lastResetTime;

private long interval;

private LongAdder token;

private int rate;

StatItem(String name, int rate, long interval) {
this.name = name;
this.rate = rate;
this.interval = interval;
this.lastResetTime = System.c谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口urrentTimeMillis();
this.token = buildLongAdder(rate);
}

public boolean isAllowable() {
long now = System.currentTimeMillis();
if (now > lastResetTime + interval) {
token = buildLongAdder(rate);
lastResetTime = now;
}

if (token.sum() < 0) {
return false;
}
token.decrement();
return宦妃全国 true;
}

public long getInterval() {
return in谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口terval;
}


public int getRate() {
return rate;
}


long getLastResetTime() {
return lastResetTime;
}

long getToken() {
return token.sum();
}

@Override
public String toString()咽喉癌 {
return new StringBuilder(32).append("StatItem ")
.append("[name=").append(name).append(", ")
.append("rate = ").append(rate).append(", ")
.append("interval = ").append(interval).append("]")
.toString();
}

private LongAdder buildLongAdder(int rate) {
L谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口ongAdd谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口er adder = new LongAdder();
adder.add(rate);
return adder;
}
​谈谈杜博的电影明星-betway体育_必威体育·betway亚洲进口
}
  • StatItem界说了LongAdder类型的token,其isAllowable办法会判别是否需求重置南山翁薄荷香token,假如需求则运用buildLongAdder重置token,不需求的话则在token.sum() < 0时回来false,假如大于等于0则递减token

小结

  • TPSLimiter界说了isAllowable办法
  • DefaultTPSLimiter完成了TPSLimiter,它使万书网用ConcurrentHashMap来存储StatItjyem,其key为URL中的serviceKey;isAllowable办法从URL中读取tps参数,默以为-1,小于0则从ConcurrentHashMap中移除,大于0则创立或许获取StatItem,调用StatI橱柜效果图tem的isAllowable(重置或递减token并回来成果)
  • StatItem界说了LongAdder类型的toke迪拉姆对人民币汇率n,其isAllowable办法会判别是否需求重置token,假如需求则运用buildLongAdder重置token,不需求的话则在token.sum() < 0时回来false,假如大于等于0则递减token

doc

  • TPSLimiter
  • DefaultTPSLimiter
  • StatItem

评论(0)