Skip to content

Commit

Permalink
Support concurrent loading of Config for different namespaces (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
zth9 committed Aug 6, 2023
1 parent 265fe49 commit 9ace7f0
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@
* @author Jason Song([email protected])
*/
public class DefaultConfigManager implements ConfigManager {
private ConfigFactoryManager m_factoryManager;
private final ConfigFactoryManager m_factoryManager;

private Map<String, Config> m_configs = Maps.newConcurrentMap();
private Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap();
private final Map<String, Config> m_configs = Maps.newConcurrentMap();
private final Map<String, ConfigFile> m_configFiles = Maps.newConcurrentMap();
private final Map<String, Object> m_configLocks = Maps.newConcurrentMap();
private final Map<String, Object> m_configFileLocks = Maps.newConcurrentMap();

public DefaultConfigManager() {
m_factoryManager = ApolloInjector.getInstance(ConfigFactoryManager.class);
}

@Override
public Config getConfig(String namespace) {
String lockKey = buildLockKey(namespace);
Object lock = m_configLocks.get(lockKey);
if (lock == null) {
synchronized (lockKey) {
lock = m_configLocks.computeIfAbsent(lockKey, k -> new Object());
}
}
Config config = m_configs.get(namespace);

if (config == null) {
synchronized (this) {
synchronized (lock) {
config = m_configs.get(namespace);

if (config == null) {
Expand All @@ -62,10 +71,17 @@ public Config getConfig(String namespace) {
@Override
public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
String namespaceFileName = String.format("%s.%s", namespace, configFileFormat.getValue());
String lockKey = buildLockKey(namespaceFileName);
Object lock = m_configFileLocks.get(lockKey);
if (lock == null) {
synchronized (lockKey) {
lock = m_configFileLocks.computeIfAbsent(lockKey, k -> new Object());
}
}
ConfigFile configFile = m_configFiles.get(namespaceFileName);

if (configFile == null) {
synchronized (this) {
synchronized (lock) {
configFile = m_configFiles.get(namespaceFileName);

if (configFile == null) {
Expand All @@ -79,4 +95,8 @@ public ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFor

return configFile;
}

private String buildLockKey(String factor) {
return (String.format("%s.%s", getClass().getName(), factor)).intern();
}
}

0 comments on commit 9ace7f0

Please sign in to comment.