Преглед изворни кода

Modify loading mechanism of authority rules: only pick first

- Comment/javadoc refinement

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao пре 6 година
родитељ
комит
6392cb0336
2 измењених фајлова са 23 додато и 15 уклоњено
  1. +9
    -8
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java
  2. +14
    -7
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java

+ 9
- 8
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRule.java Прегледај датотеку

@@ -21,14 +21,15 @@ import com.alibaba.csp.sentinel.node.DefaultNode;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

/***
*
/**
* @author youji.zj
*/
public class AuthorityRule extends AbstractRule {

/*** 0代表白名单;1代表黑名单 ***/
private int strategy;
/**
* Mode: 0 for whitelist; 1 for blacklist.
*/
private int strategy = RuleConstant.AUTHORITY_WHITE;

public int getStrategy() {
return strategy;
@@ -60,12 +61,12 @@ public class AuthorityRule extends AbstractRule {
public boolean passCheck(Context context, DefaultNode node, int count, Object... args) {
String requester = context.getOrigin();

// 来源或者限流的应用为null直接通过
if (StringUtil.isEmpty(requester) || this.getLimitApp() == null) {
// Empty origin or empty limitApp will pass.
if (StringUtil.isEmpty(requester) || StringUtil.isEmpty(this.getLimitApp())) {
return true;
}

// 白名单、黑名单列表为逗号分隔的应用列表, indexOf还不行,需要精确匹配
// Do exact match with origin name.
int pos = this.getLimitApp().indexOf(requester);
boolean contain = pos > -1;

@@ -99,6 +100,6 @@ public class AuthorityRule extends AbstractRule {
"resource=" + getResource() +
", limitApp=" + getLimitApp() +
", strategy=" + strategy +
"} " + super.toString();
"} ";
}
}

+ 14
- 7
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/authority/AuthorityRuleManager.java Прегледај датотеку

@@ -31,9 +31,12 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;

/***
/**
* Manager for authority rules.
*
* @author youji.zj
* @author jialiang.linjl
* @author Eric Zhao
*/
public class AuthorityRuleManager {

@@ -60,7 +63,9 @@ public class AuthorityRuleManager {
}

/**
* @param rules
* Load the authority rules to memory.
*
* @param rules list of authority rules
*/
public static void loadRules(List<AuthorityRule> rules) {
currentProperty.updateValue(rules);
@@ -114,7 +119,7 @@ public class AuthorityRuleManager {
if (rules != null) {
authorityRules.putAll(rules);
}
RecordLog.info("receive authority config: " + authorityRules);
RecordLog.info("[AuthorityRuleManager] Authority rules received: " + authorityRules);
}

private Map<String, List<AuthorityRule>> loadAuthorityConf(List<AuthorityRule> list) {
@@ -129,12 +134,15 @@ public class AuthorityRuleManager {

String identity = rule.getResource();
List<AuthorityRule> ruleM = newRuleMap.get(identity);
// putIfAbsent
if (ruleM == null) {
ruleM = new ArrayList<AuthorityRule>();
ruleM.add(rule);
newRuleMap.put(identity, ruleM);
} else {
// One resource should only have at most one authority rule, so just ignore redundant rules.
RecordLog.warn("[AuthorityRuleManager] Ignoring redundant rule: " + rule.toString());
}
ruleM.add(rule);

}

return newRuleMap;
@@ -148,9 +156,8 @@ public class AuthorityRuleManager {
if (rules != null) {
authorityRules.putAll(rules);
}
RecordLog.info("load authority config: " + authorityRules);
RecordLog.info("[AuthorityRuleManager] Load authority rules: " + authorityRules);
}

}

}

Loading…
Откажи
Сачувај