Skip to content

Commit

Permalink
Improve AuthorityRuleManager: replace the rule map instead of clear-t…
Browse files Browse the repository at this point in the history
…hen-insert when updating rules (#2655)
  • Loading branch information
zhuyou1234 authored and sczyh30 committed Apr 6, 2022
1 parent fcc539d commit 679625e
Showing 1 changed file with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
public final class AuthorityRuleManager {

private static Map<String, Set<AuthorityRule>> authorityRules = new ConcurrentHashMap<>();
private static volatile Map<String, Set<AuthorityRule>> authorityRules = new ConcurrentHashMap<>();

private static final RulePropertyListener LISTENER = new RulePropertyListener();
private static SentinelProperty<List<AuthorityRule>> currentProperty = new DynamicSentinelProperty<>();
Expand Down Expand Up @@ -92,13 +92,16 @@ public static List<AuthorityRule> getRules() {
private static class RulePropertyListener implements PropertyListener<List<AuthorityRule>> {

@Override
public void configUpdate(List<AuthorityRule> conf) {
Map<String, Set<AuthorityRule>> rules = loadAuthorityConf(conf);
public synchronized void configLoad(List<AuthorityRule> value) {
authorityRules = loadAuthorityConf(value);

authorityRules.clear();
if (rules != null) {
authorityRules.putAll(rules);
}
RecordLog.info("[AuthorityRuleManager] Authority rules loaded: {}", authorityRules);
}

@Override
public synchronized void configUpdate(List<AuthorityRule> conf) {
authorityRules = loadAuthorityConf(conf);

RecordLog.info("[AuthorityRuleManager] Authority rules received: {}", authorityRules);
}

Expand Down Expand Up @@ -135,16 +138,6 @@ private Map<String, Set<AuthorityRule>> loadAuthorityConf(List<AuthorityRule> li
return newRuleMap;
}

@Override
public void configLoad(List<AuthorityRule> value) {
Map<String, Set<AuthorityRule>> rules = loadAuthorityConf(value);

authorityRules.clear();
if (rules != null) {
authorityRules.putAll(rules);
}
RecordLog.info("[AuthorityRuleManager] Load authority rules: {}", authorityRules);
}
}

static Map<String, Set<AuthorityRule>> getAuthorityRules() {
Expand Down

0 comments on commit 679625e

Please sign in to comment.