Improvement for demo for flow control by relation | 关联流量控制Demo
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
### Describe what happened (or what feature you want)
找了很多地方,没有找到关联流量控制的Demo,这里补充下。
这里先需要说明一下,关联流量控制生效的时间点为:B>threashold的时候A就会被限流。
```java
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.Arrays;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
*
* 钉钉:1xd_zqcim4jtj5
*
* @author westonlv
* @since 2019/2/21 11:05
*/
public class FlowQpsRelateDemo {
static String node_read = "read";
static String node_write = "write";
public static void main(String[] args) {
FlowRule rule = new FlowRule();
rule.setResource(node_read);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setStrategy(RuleConstant.STRATEGY_RELATE);
rule.setCount(10);
rule.setRefResource(node_write);
FlowRuleManager.loadRules(Arrays.asList(rule));
CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
Thread thread1 = new Thread(() -> {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++) {
ContextUtil.enter("read");
Entry entry = null;
try {
entry = SphU.entry(node_read);
Thread.sleep(100);
System.out.println("read pass..");
} catch (BlockException e) {
System.out.println("block read:" + (i - 10));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (null != entry)
entry.exit();
ContextUtil.exit();
}
}
System.out.println("read over..");
});
thread1.start();
Thread thread2 = new Thread(() -> {
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int i = 0; i < 20; i++) {
ContextUtil.enter("write");
Entry entry = null;
try {
entry = SphU.entry(node_write);
Thread.sleep(1);
System.out.println("write pass..");
} catch (BlockException e) {
System.out.println("block write:" + (i - 10));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (null != entry)
entry.exit();
ContextUtil.exit();
}
}
System.out.println("write over..");
});
thread2.start();
}
}
```
===========================下方为测试结果============================
可以控制read资源和write资源sleep的时间产生不同的结果
```
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write pass..
write over..
read pass..
block read:-9
block read:-8
block read:-7
block read:-6
block read:-5
block read:-4
block read:-3
block read:-2
block read:-1
block read:0
block read:1
block read:2
block read:3
block read:4
block read:5
block read:6
block read:7
block read:8
block read:9
read over..
```
Contributor guide
Research direction
Start by reviewing the issue's FlowQpsRelateDemo main entry point and the repository's existing flow-control demos to identify the appropriate documentation or example location. Done means the relation-based flow-control scenario is added with the stated B-threshold timing explanation and its demonstrated read/write behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend, documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100