From bff4bcf75115351814de24b9f14ce4d17a90f2c4 Mon Sep 17 00:00:00 2001 From: tian90coder Date: Sat, 12 Aug 2023 22:23:52 +0800 Subject: [PATCH] Support concurrent loading of Config for different namespaces (#29) --- CHANGES.md | 1 + .../framework/apollo/internals/DefaultConfigManager.java | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dbca21d5..20674bca 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,5 +7,6 @@ Apollo Java 2.2.0 ------------------ [refactor(apollo-client): Optimize the exception message when failing to retrieve configuration information.](https://github.com/apolloconfig/apollo-java/pull/22) [Add JUnit5 extension support for apollo mock server.](https://github.com/apolloconfig/apollo-java/pull/25) +[Support concurrent loading of Config for different namespaces.](https://github.com/apolloconfig/apollo-java/pull/31) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/2?closed=1) \ No newline at end of file diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java index f583a915..e897b6f7 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfigManager.java @@ -33,7 +33,9 @@ public class DefaultConfigManager implements ConfigManager { private ConfigFactoryManager m_factoryManager; private Map m_configs = Maps.newConcurrentMap(); + private Map m_configLocks = Maps.newConcurrentMap(); private Map m_configFiles = Maps.newConcurrentMap(); + private Map m_configFileLocks = Maps.newConcurrentMap(); public DefaultConfigManager() { m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class); @@ -44,7 +46,8 @@ public Config getConfig(String namespace) { Config config = m_configs.get(namespace); if (config == null) { - synchronized (this) { + Object lock = m_configLocks.computeIfAbsent(namespace, key -> new Object()); + synchronized (lock) { config = m_configs.get(namespace); if (config == null) { @@ -65,7 +68,8 @@ public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFor ConfigFile configFile = m_configFiles.get(namespaceFileName); if (configFile == null) { - synchronized (this) { + Object lock = m_configFileLocks.computeIfAbsent(namespaceFileName, key -> new Object()); + synchronized (lock) { configFile = m_configFiles.get(namespaceFileName); if (configFile == null) {