Sentinel源码分析----流控规则与FlowSlot

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是一个全量的规则列表,例如我在控制台配置了如下的规则:
image.png
那么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 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值