FlowSlot主要是用来进行流控规则的处理,直接看下代码
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
boolean prioritized, Object... args) throws Throwable {
checkFlow(resourceWrapper, context, node, count, prioritized);
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int count, boolean prioritized) throws BlockException {
// 获取流控规则
Map<String, List<FlowRule>> flowRules = FlowRuleManager.getFlowRuleMap();
//通过资源名称来获取规则列表
List<FlowRule> rules = flowRules.get(resource.getName());
if (rules != null) {
for (FlowRule rule : rules) {
// 遍历规则进行处理
if (!canPassCheck(rule, context, node, count, prioritized)) {
// 如果规则校验不通过,那么抛出FlowException异常
throw new FlowException(rule.getLimitApp(), rule);
}
}
}
}
boolean canPassCheck(FlowRule rule, Context context, DefaultNode node, int count, boolean prioritized) {
// 交由FlowRuleChecker进行逻辑处理
return FlowRuleChecker.passCheck(rule, context, node, count, prioritized);
}
这里的flowRules是一个全量的规则列表,例如我在控制台配置了如下的规则:
那么flowRules中就有两个元素,key分别是test和hello,对应的值是一个集合,集合里只有一个元素,就是实际的规则实体FlowRule,具体值与我们配置相关,看下FlowRule中有哪些字段
public class FlowRule extends AbstractRule {
public FlowRule() {
super();
setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}
public FlowRule(String resourceName) {
super();
setResource(resourceName);
setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);
}
private int grade = RuleConstant.FLOW_GRADE_QPS;
private double count;
private int strategy = RuleConstant.STRATEGY_DIRECT;
private String refResource;
private int controlBehavior = RuleConstant.CONTROL_BEHAVIOR_DEFAULT;
private int warmUpPeriodSec = 10;
private int maxQueueingTimeMs