[perf] Cache initTenantCache operation in Context for performance

This commit is contained in:
Sergey Konovalov
2025-10-08 21:52:06 +03:00
parent 1c666d5776
commit 63055ad2d2
3 changed files with 27 additions and 3 deletions

View File

@ -39,6 +39,8 @@ const tenantManager = require('./tenantManager');
const runtimeConfigManager = require('./runtimeConfigManager');
const moduleReloader = require('./moduleReloader');
let configCache = null;
function Context() {
this.logger = logger.getLogger('nodeJS');
this.initDefault();
@ -99,12 +101,27 @@ Context.prototype.initFromPubSub = function (data) {
this.init(ctx.tenant, ctx.docId, ctx.userId, ctx.shardKey, ctx.wopiSrc, ctx.userSessionId);
};
Context.prototype.initTenantCache = async function () {
const runtimeConfig = await runtimeConfigManager.getConfig(this);
const tenantConfig = await tenantManager.getTenantConfig(this);
this.config = utils.deepMergeObjects({}, moduleReloader.getBaseConfig(), runtimeConfig, tenantConfig);
if (!configCache) {
configCache = Object.create(null);
}
this.config = configCache[this.tenant];
if (!this.config) {
const runtimeConfig = await runtimeConfigManager.getConfig(this);
const tenantConfig = await tenantManager.getTenantConfig(this);
this.config = utils.deepMergeObjects({}, moduleReloader.getBaseConfig(), runtimeConfig, tenantConfig);
configCache[this.tenant] = this.config;
}
//todo license and secret
};
Context.prototype.cleanRuntimeConfigCache = function () {
configCache = null;
};
Context.prototype.cleanTenantConfigCache = function (tenant) {
if (configCache) {
configCache[tenant] = null;
}
};
Context.prototype.setTenant = function (tenant) {
this.tenant = tenant;

View File

@ -146,6 +146,8 @@ function handleConfigFileChange(eventTypeOrCurrent, filenameOrPrevious) {
reloadTimer = null;
nodeCache.del(configFileName);
operationContext.global.logger.info(`handleConfigFileChange reloading config: ${configFileName}`);
operationContext.global.cleanRuntimeConfigCache();
getConfig(operationContext.global)
.then(config => {
logger.configureLogger(config?.log?.options);

View File

@ -116,6 +116,7 @@ async function getTenantConfig(ctx) {
} catch (e) {
ctx.logger.debug('getTenantConfig error: %s', e.stack);
} finally {
ctx.cleanTenantConfigCache(ctx.tenant);
nodeCache.set(configPath, res);
}
}
@ -135,6 +136,8 @@ async function setTenantConfig(ctx, config) {
const tenantPath = utils.removeIllegalCharacters(ctx.tenant);
const configPath = path.join(cfgTenantsBaseDir, tenantPath, cfgTenantsFilenameConfig);
await writeFile(configPath, JSON.stringify(newConfig, null, 2), 'utf8');
ctx.cleanTenantConfigCache(ctx.tenant);
nodeCache.set(configPath, newConfig);
}
return newConfig;
@ -151,6 +154,8 @@ async function replaceTenantConfig(ctx, config) {
const tenantPath = utils.removeIllegalCharacters(ctx.tenant);
const configPath = path.join(cfgTenantsBaseDir, tenantPath, cfgTenantsFilenameConfig);
await writeFile(configPath, JSON.stringify(config, null, 2), 'utf8');
ctx.cleanTenantConfigCache(ctx.tenant);
nodeCache.set(configPath, config);
return config;
}