Skip to content

Commit

Permalink
Modify loading mechanism of authority rules: only pick first
Browse files Browse the repository at this point in the history
- Comment/javadoc refinement

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Aug 13, 2018
1 parent 4226868 commit 6392cb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
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;
Expand Down Expand Up @@ -60,12 +61,12 @@ public int hashCode() {
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;

Expand Down Expand Up @@ -99,6 +100,6 @@ public String toString() {
"resource=" + getResource() +
", limitApp=" + getLimitApp() +
", strategy=" + strategy +
"} " + super.toString();
"} ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
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 {

Expand All @@ -60,7 +63,9 @@ public static void register2Property(SentinelProperty<List<AuthorityRule>> prope
}

/**
* @param rules
* Load the authority rules to memory.
*
* @param rules list of authority rules
*/
public static void loadRules(List<AuthorityRule> rules) {
currentProperty.updateValue(rules);
Expand Down Expand Up @@ -114,7 +119,7 @@ public void configUpdate(List<AuthorityRule> conf) {
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) {
Expand All @@ -129,12 +134,15 @@ private Map<String, List<AuthorityRule>> loadAuthorityConf(List<AuthorityRule> l

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;
Expand All @@ -148,9 +156,8 @@ public void configLoad(List<AuthorityRule> value) {
if (rules != null) {
authorityRules.putAll(rules);
}
RecordLog.info("load authority config: " + authorityRules);
RecordLog.info("[AuthorityRuleManager] Load authority rules: " + authorityRules);
}

}

}

0 comments on commit 6392cb0

Please sign in to comment.