聊一聊dubbo的TPSLimiter-必威手机版_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聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 current invocatio聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 n is allowed by TPS rule
*
* @param url url
* @param 聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 invocation invocation
* @return true allow the current invocation, otherwise, return false
*/
boolean 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/DefaultTPSLimit上海市天气预报er.java

public class DefaultTPSLimiter implements TPSLimiter {

聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 private final ConcurrentMap stats = new C昆明地铁oncurrentHashMap();

@Override
public boolean isAllowa鸡蛋煮多久能熟ble(URL url, Invocation invocation) {
int rate = url聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 .getParameter(TPS_LIMIT_RATE_KEY, -1);
long interval = url.getParameter(TPS_LIMIT_INTERVAL_KEY, DEFAULT_TPS_LIMIT_INTERVAL);
String serviceKey = url.getServiceKey();
if (rate > 0) {
StatItem statItem = stats.get(serviceKey);
if (statItem == null) {
stats.putIfAbsent(serviceKey, new StatItem(serviceKey, rate, interval));
statItem = stats.get(ser哑铃训练办法viceKey);
} else {
//rate or interval has changed, rebuild
if (statItem.getRate() != rate || statItem.getInterval() != interval) {
stats.put(serviceKey, new StatItem(serviceKey, rate, interval));
statItem = st竹山天气预报ats.get(serviceKey);
}
}
return statItem.isAllowable();
} else {
StatItem statItem = stat将军家的小娘子s.get(serviceKey);
ivga接口f (statItem != null) {
stats.remove(serviceKey);
}
}

return true;
}

}
  • DefaultTPSLimiter完成了TPSLimiter,它运用ConcurrentHashMap来存储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 Str替代姐姐ing name;

private long lastReset饿了么商家版Time;

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 =developpesex System.currentTimeMillis();
this.token = buildLongAdder(rate);
}

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

if (token.sum() < 0) {
return false;
}
token.decre客房服务员m皖南事变ent();
return true;
}

public long getInterval() {
return interval;
}


public int getRate() {
return rate;
}


long getLastResetTime() {
return lastResetTime;
}

lon微hg getToken() {
return token.sum();
}

@Override
public String toString() {
return new StringBuilder(3聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 2).append("StatItem ")
.append("[name=").append(name).append(", ")
.append("rate = ").append(rate).append(", ")
.append("interval = ").append(interval).append("]")
.toString();
}

wallprivate LongAdder buildLongAdder(int rate) {
LongAdder adder = new LongAdder();
adder.add(rate);
ret聊一聊dubbo的TPSLimiter-必威手机版_betway体育手机版_betway体育手机版 urn adder;
}

}
  • StatItem界说了LongAdder类型的token,其isAllowable办法会判别是否需求重置token,假如需求则运用buildLongAdder重置token,不需求的话则在token.sum() < 0时回来false,假如大于等于0则递减token

小结

  • TPSLimiter界说了isAllowable办法
  • DefaultTPSLimi奇点ter完成了TPSLimiter,它运用ConcurrentHashMap来存储StatItem,其key为URL中的serviceKey;isAllowable办法从URL中读取tps参数,默以为-1,小于0则从ConcurrentHashMap中移除,大于0则创立或许获取StatItem,调用StatItem的isAllowable(重置或递减token并回来成果)
  • StatI91pontem界说了LongAdder类型的token,其isAllowable办法会判别是否需求重置token,假如需求则运用buildLongAdder重置token,不需求的话则在token.sum() < 0时回来false,假如大于等于0则递减token

doc

  • TPSLimiter
  • DefaultTPS狼图腾Limiter
  • StatItem

评论(0)