Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问这个是我单测写的有问题吗?我限流设置的是1,但是多线程访问进入了线程数的请求。 #3449

Open
aa310958153 opened this issue Sep 12, 2024 · 4 comments

Comments

@aa310958153
Copy link

正常我的理解只会打印一个helloword
/**
* 初始化流控规则
*/
private static void initFlowRules() {

    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource("HelloWorld");

    ClusterFlowConfig config = new ClusterFlowConfig();
    config.setFallbackToLocalWhenFail(true);
    config.setSampleCount(10);
    config.setThresholdType(0);
    config.setWindowIntervalMs(1000);
    rule1.setClusterConfig(config);
    // set limit qps to 20
    rule1.setCount(1);
    rule1.setStrategy(0);
    rule1.setClusterMode(false);
    rule1.setControlBehavior(0);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}

@test
public void flowRule_TEST() throws InterruptedException {

    // 配置规则.
    initFlowRules();

    for (int j = 0; j < 4; j++) {
        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                for (int k = 0; k < 30; k++) {
                    // 1.5.0 版本开始可以直接利用 try-with-resources 特性 自动回收调用  entry.exit() 配合注解使用 @SentinelResource("HelloWorld")
                    try (Entry entry = SphU.entry(
                        "HelloWorld",
                        EntryType.IN)) {
                        // 被保护的逻辑
                        System.out.println(Thread.currentThread().getId() + "hello world" + (++i));

                    } catch (BlockException ex) {
                        i++;
                        // 处理被流控的逻辑
                        System.out.println("blocked!");

                    }
                }

            }
        }).start();

    }
    Thread.sleep(500000000);
}

打印日志
25hello world1
24hello world1
26hello world1
23hello world1
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!
blocked!

@aa310958153
Copy link
Author

sentinel-core com.alibaba.csp 1.8.8

@S0Louis
Copy link

S0Louis commented Sep 20, 2024

我也好奇这个问题,源码层面是先执行限流规则判断,再累加统计信息,所以多线程场景下会出现这个问题

@S0Louis
Copy link

S0Louis commented Sep 21, 2024

找了一个历史的issues:#59

@aa310958153
Copy link
Author

找了一个历史的issues:#59

非常感谢您的回答,我了解一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants