Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

qps/并发线程数 都无法限流

Open WuJingLearn opened this issue 4 years ago • 3 comments

  private static void initFlowQpsRule() {
        List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule1 = new FlowRule();
        rule1.setResource("hello");
        rule1.setCount(4);
        rule1.setGrade(RuleConstant.FLOW_GRADE_THREAD);
        rule1.setLimitApp("default");
        rules.add(rule1);
        FlowRuleManager.loadRules(rules);
    }

   public static void main(String[] args) {
        initFlowQpsRule();

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                entry();
            }).start();
        }
    }

 public static void entry() {
        Entry methodA = null;
        try {
            methodA = SphU.entry("hello");
            System.out.println(Thread.currentThread().getName() + "访问到系统资源");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "退出");
        } catch (BlockException e1) {
            System.out.println(Thread.currentThread() + "被限流了");
        } catch (Exception e2) {
        } finally {
            if (methodA != null) {
                methodA.exit();
            }
        }
    }


如上文:设置了并发阈值为4,开启100个线程同时访问。结果都访问到资源,没有被限流 Thread-16访问到系统资源 Thread-97访问到系统资源 Thread-7访问到系统资源 Thread-48访问到系统资源 Thread-78访问到系统资源 Thread-17访问到系统资源 Thread-73访问到系统资源 Thread-96访问到系统资源 Thread-23访问到系统资源 Thread-98访问到系统资源 Thread-13访问到系统资源 Thread-12访问到系统资源 Thread-5访问到系统资源 Thread-74访问到系统资源 Thread-19访问到系统资源 Thread-90访问到系统资源 Thread-53访问到系统资源 Thread-50访问到系统资源 Thread-71访问到系统资源 Thread-0访问到系统资源 Thread-34访问到系统资源 Thread-1访问到系统资源 Thread-93访问到系统资源

WuJingLearn avatar Dec 25 '21 02:12 WuJingLearn

遇到了相同问题,应该是初始化过程非线程安全导致。 尝试首次访问资源后,观察后续流控情况,应该符合测试预期结果。

kangzhixing avatar Dec 29 '21 05:12 kangzhixing

遇到了相同问题,应该是初始化过程非线程安全导致。 尝试首次访问资源后,观察后续流控情况,应该符合测试预期结果。 是的,看了一下代码,在canPass的时候拿到当前通过的qps+1来和阈值比较。所以几乎同时访问的话,会导致这样不准

WuJingLearn avatar Dec 29 '21 05:12 WuJingLearn

应该跟初始化过程没关系,初始化后符合预期可能是并发不够,增大并发 `public class Hi {

private static void initFlowQpsRule() {
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule rule1 = new FlowRule();
    rule1.setResource("hello");
    rule1.setCount(4);
    rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule1.setLimitApp("default");
    rules.add(rule1);
    FlowRuleManager.loadRules(rules);
}

public static void main(String[] args) throws BlockException {
    initFlowQpsRule();
    int thread=20;
    CountDownLatch latch=new CountDownLatch(thread);
    SphU.entry("hello");
    for (int i = 0; i < thread; i++) {
        new Thread(()->{
            latch.countDown();
            try {
                latch.await();
                entry();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();
    }
}

public static void entry() {
    Entry methodA = null;
    try {
        methodA = SphU.entry("hello");
        System.out.println(Thread.currentThread().getName() + "访问到系统资源");
        Thread.sleep(2000);
        System.out.println(Thread.currentThread().getName() + "退出");
    } catch (BlockException e1) {
        System.out.println(Thread.currentThread() + "被限流了");
    } catch (Exception e2) {
    } finally {
        if (methodA != null) {
            methodA.exit();
        }
    }
}

} ` image

PepoRobert avatar Dec 29 '21 07:12 PepoRobert