@@ -88,8 +88,8 @@ public class CtSph implements Sph { | |||
ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper); | |||
/* | |||
* Means processor size exceeds {@link Constants.MAX_ENTRY_SIZE}, no | |||
* rule checking will do. | |||
* Means processor cache size exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE}, so no | |||
* rule checking will be done. | |||
*/ | |||
if (chain == null) { | |||
return new CtEntry(resourceWrapper, null, context); | |||
@@ -102,7 +102,7 @@ public class CtSph implements Sph { | |||
e.exit(count, args); | |||
throw e1; | |||
} catch (Throwable e1) { | |||
RecordLog.info("sentinel unexpected exception", e1); | |||
RecordLog.info("Sentinel unexpected exception", e1); | |||
} | |||
return e; | |||
} | |||
@@ -134,8 +134,7 @@ public class CtSph implements Sph { | |||
} | |||
chain = Env.slotsChainbuilder.build(); | |||
HashMap<ResourceWrapper, ProcessorSlotChain> newMap | |||
= new HashMap<ResourceWrapper, ProcessorSlotChain>( | |||
Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>( | |||
chainMap.size() + 1); | |||
newMap.putAll(chainMap); | |||
newMap.put(resourceWrapper, chain); | |||
@@ -53,13 +53,23 @@ public class FlowRule extends AbstractRule { | |||
*/ | |||
private int grade = RuleConstant.FLOW_GRADE_QPS; | |||
/** | |||
* Flow control threshold count. | |||
*/ | |||
private double count; | |||
/** | |||
* 0为直接限流;1为关联限流;2为链路限流 | |||
* Flow control strategy based on invocation chain. | |||
* | |||
* {@link RuleConstant#STRATEGY_DIRECT} for direct flow control (by origin); | |||
* {@link RuleConstant#STRATEGY_RELATE} for relevant flow control (with relevant resource); | |||
* {@link RuleConstant#STRATEGY_CHAIN} for chain flow control (by entrance resource). | |||
*/ | |||
private int strategy = RuleConstant.STRATEGY_DIRECT; | |||
/** | |||
* Reference resource in flow control with relevant resource. | |||
*/ | |||
private String refResource; | |||
/** | |||
@@ -86,9 +86,6 @@ public class FlowRuleManager { | |||
*/ | |||
public static List<FlowRule> getRules() { | |||
List<FlowRule> rules = new ArrayList<FlowRule>(); | |||
if (flowRules == null) { | |||
return rules; | |||
} | |||
for (Map.Entry<String, List<FlowRule>> entry : flowRules.entrySet()) { | |||
rules.addAll(entry.getValue()); | |||
} | |||
@@ -145,13 +142,11 @@ public class FlowRuleManager { | |||
public static void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int count) | |||
throws BlockException { | |||
if (flowRules != null) { | |||
List<FlowRule> rules = flowRules.get(resource.getName()); | |||
if (rules != null) { | |||
for (FlowRule rule : rules) { | |||
if (!rule.passCheck(context, node, count)) { | |||
throw new FlowException(rule.getLimitApp()); | |||
} | |||
List<FlowRule> rules = flowRules.get(resource.getName()); | |||
if (rules != null) { | |||
for (FlowRule rule : rules) { | |||
if (!rule.passCheck(context, node, count)) { | |||
throw new FlowException(rule.getLimitApp()); | |||
} | |||
} | |||
} | |||
@@ -166,14 +161,12 @@ public class FlowRuleManager { | |||
return false; | |||
} | |||
if (flowRules != null) { | |||
List<FlowRule> rules = flowRules.get(resourceName); | |||
List<FlowRule> rules = flowRules.get(resourceName); | |||
if (rules != null) { | |||
for (FlowRule rule : rules) { | |||
if (origin.equals(rule.getLimitApp())) { | |||
return false; | |||
} | |||
if (rules != null) { | |||
for (FlowRule rule : rules) { | |||
if (origin.equals(rule.getLimitApp())) { | |||
return false; | |||
} | |||
} | |||
} | |||
@@ -190,7 +183,7 @@ public class FlowRuleManager { | |||
flowRules.clear(); | |||
flowRules.putAll(rules); | |||
} | |||
RecordLog.info("receive flow config: " + flowRules); | |||
RecordLog.info("[FlowRuleManager] Flow rules received: " + flowRules); | |||
} | |||
@Override | |||
@@ -200,7 +193,7 @@ public class FlowRuleManager { | |||
flowRules.clear(); | |||
flowRules.putAll(rules); | |||
} | |||
RecordLog.info("load flow config: " + flowRules); | |||
RecordLog.info("[FlowRuleManager] Flow rules loaded: " + flowRules); | |||
} | |||
} | |||
@@ -128,7 +128,7 @@ public class NodeSelectorSlot extends AbstractLinkedProcessorSlot<Object> { | |||
/** | |||
* {@link DefaultNode}s of the same resource in different context. | |||
*/ | |||
private Map<String, DefaultNode> map = new HashMap<String, DefaultNode>(10); | |||
private volatile Map<String, DefaultNode> map = new HashMap<String, DefaultNode>(10); | |||
@Override | |||
public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args) | |||
@@ -10,8 +10,4 @@ | |||
</parent> | |||
<artifactId>sentinel-demo-basic</artifactId> | |||
<dependencies> | |||
</dependencies> | |||
</project> |
@@ -42,18 +42,19 @@ public class FlowQpsDemo { | |||
private static volatile boolean stop = false; | |||
private static final int threadCount = 1; | |||
private static final int threadCount = 32; | |||
private static int seconds = 60 + 40; | |||
public static void main(String[] args) throws Exception { | |||
initFlowQpsRule(); | |||
tick(); | |||
// first make the system run on a very low condition | |||
simulateTraffic(); | |||
System.out.println("===== begin to do flow control"); | |||
System.out.println("only 20 requests per second can pass"); | |||
initFlowQpsRule(); | |||
} | |||