fix(core): Prevent occassional 429s on license init in multi-main setup (#9284)

This commit is contained in:
Iván Ovejero
2024-05-06 09:04:16 +02:00
committed by GitHub
parent bfb0eb7a06
commit 22b6f90950
5 changed files with 124 additions and 10 deletions

View File

@@ -41,6 +41,26 @@ export class License {
private readonly usageMetricsService: UsageMetricsService,
) {}
/**
* Whether this instance should renew the license - on init and periodically.
*/
private renewalEnabled(instanceType: N8nInstanceType) {
if (instanceType !== 'main') return false;
const autoRenewEnabled = config.getEnv('license.autoRenewEnabled');
/**
* In multi-main setup, all mains start off with `unset` status and so renewal disabled.
* On becoming leader or follower, each will enable or disable renewal, respectively.
* This ensures the mains do not cause a 429 (too many requests) on license init.
*/
if (config.getEnv('multiMainSetup.enabled')) {
return autoRenewEnabled && config.getEnv('multiMainSetup.instanceType') === 'leader';
}
return autoRenewEnabled;
}
async init(instanceType: N8nInstanceType = 'main') {
if (this.manager) {
this.logger.warn('License manager already initialized or shutting down');
@@ -53,7 +73,6 @@ export class License {
const isMainInstance = instanceType === 'main';
const server = config.getEnv('license.serverUrl');
const autoRenewEnabled = isMainInstance && config.getEnv('license.autoRenewEnabled');
const offlineMode = !isMainInstance;
const autoRenewOffset = config.getEnv('license.autoRenewOffset');
const saveCertStr = isMainInstance
@@ -66,13 +85,15 @@ export class License {
? async () => await this.usageMetricsService.collectUsageMetrics()
: async () => [];
const renewalEnabled = this.renewalEnabled(instanceType);
try {
this.manager = new LicenseManager({
server,
tenantId: config.getEnv('license.tenantId'),
productIdentifier: `n8n-${N8N_VERSION}`,
autoRenewEnabled,
renewOnInit: autoRenewEnabled,
autoRenewEnabled: renewalEnabled,
renewOnInit: renewalEnabled,
autoRenewOffset,
offlineMode,
logger: this.logger,
@@ -126,7 +147,7 @@ export class License {
if (this.orchestrationService.isMultiMainSetupEnabled && !isMultiMainLicensed) {
this.logger.debug(
'[Multi-main setup] License changed with no support for multi-main setup - no new followers will be allowed to init. To restore multi-main setup, please upgrade to a license that supporst this feature.',
'[Multi-main setup] License changed with no support for multi-main setup - no new followers will be allowed to init. To restore multi-main setup, please upgrade to a license that supports this feature.',
);
}
}
@@ -335,4 +356,9 @@ export class License {
isWithinUsersLimit() {
return this.getUsersLimit() === UNLIMITED_LICENSE_QUOTA;
}
async reinit() {
this.manager?.reset();
await this.init();
}
}