Java Code Examples for com.alibaba.csp.sentinel.slots.block.RuleConstant#FLOW_GRADE_THREAD

The following examples show how to use com.alibaba.csp.sentinel.slots.block.RuleConstant#FLOW_GRADE_THREAD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParamFlowChecker.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
static boolean passSingleValueCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount,
                                    Object value) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        if (rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) {
            return passThrottleLocalCheck(resourceWrapper, rule, acquireCount, value);
        } else {
            return passDefaultLocalCheck(resourceWrapper, rule, acquireCount, value);
        }
    } else if (rule.getGrade() == RuleConstant.FLOW_GRADE_THREAD) {
        Set<Object> exclusionItems = rule.getParsedHotItems().keySet();
        long threadCount = getParameterMetric(resourceWrapper).getThreadCount(rule.getParamIdx(), value);
        if (exclusionItems.contains(value)) {
            int itemThreshold = rule.getParsedHotItems().get(value);
            return ++threadCount <= itemThreshold;
        }
        long threshold = (long)rule.getCount();
        return ++threadCount <= threshold;
    }

    return true;
}
 
Example 2
Source File: ParamFlowChecker.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
static boolean passSingleValueCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount,
                                    Object value) {
    if (rule.getGrade() == RuleConstant.FLOW_GRADE_QPS) {
        if (rule.getControlBehavior() == RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER) {
            return passThrottleLocalCheck(resourceWrapper, rule, acquireCount, value);
        } else {
            return passDefaultLocalCheck(resourceWrapper, rule, acquireCount, value);
        }
    } else if (rule.getGrade() == RuleConstant.FLOW_GRADE_THREAD) {
        Set<Object> exclusionItems = rule.getParsedHotItems().keySet();
        long threadCount = getParameterMetric(resourceWrapper).getThreadCount(rule.getParamIdx(), value);
        if (exclusionItems.contains(value)) {
            int itemThreshold = rule.getParsedHotItems().get(value);
            return ++threadCount <= itemThreshold;
        }
        long threshold = (long)rule.getCount();
        return ++threadCount <= threshold;
    }

    return true;
}
 
Example 3
Source File: DefaultControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPassForThreadCount() {
    int threshold = 8;
    TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_THREAD);
    Node node = mock(Node.class);
    when(node.curThreadNum()).thenReturn(threshold - 1)
        .thenReturn(threshold);

    assertTrue(controller.canPass(node, 1));
    assertFalse(controller.canPass(node, 1));
}
 
Example 4
Source File: DefaultControllerTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPassForThreadCount() {
    int threshold = 8;
    TrafficShapingController controller = new DefaultController(threshold, RuleConstant.FLOW_GRADE_THREAD);
    Node node = mock(Node.class);
    when(node.curThreadNum()).thenReturn(threshold - 1)
        .thenReturn(threshold);

    assertTrue(controller.canPass(node, 1));
    assertFalse(controller.canPass(node, 1));
}
 
Example 5
Source File: DefaultController.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
private int avgUsedTokens(Node node) {
    if (node == null) {
        return DEFAULT_AVG_USED_TOKENS;
    }
    return grade == RuleConstant.FLOW_GRADE_THREAD ? node.curThreadNum() : (int)(node.passQps());
}
 
Example 6
Source File: DefaultController.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
private int avgUsedTokens(Node node) {
    if (node == null) {
        return DEFAULT_AVG_USED_TOKENS;
    }
    return grade == RuleConstant.FLOW_GRADE_THREAD ? node.curThreadNum() : (int)(node.passQps());
}