Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion onboarding-enabler-nodejs/src/EurekaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import ConfigClusterResolver from './ConfigClusterResolver.js';
import DnsClusterResolver from './DnsClusterResolver.js';
import Logger from './Logger.js';
import defaultConfig from './defaultConfig.js';
import envConfig from './envConfig.js';
import https from 'https';

function noop() {}
Expand Down Expand Up @@ -113,7 +114,7 @@ export default class Eureka extends EventEmitter {
const envYml = getYaml(path.join(cwd, `${filename}-${env}.yml`));

// apply config overrides in appropriate order
this.config = merge({}, defaultConfig, defaultYml, envYml, config);
this.config = merge({}, defaultConfig, defaultYml, envYml, envConfig(), config);

// Validate the provided the values we need:
this.validateConfig(this.config);
Expand Down
55 changes: 55 additions & 0 deletions onboarding-enabler-nodejs/src/envConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/

// envConfig.js — maps EUREKA_CLIENT_* env vars to eureka config properties
import Logger from './Logger.js';

const logger = new Logger();

const ENV_MAP = [
{
env: 'EUREKA_CLIENT_REGISTRYFETCHINTERVALSECONDS',
key: 'registryFetchInterval',
unit: 'seconds',
},
{
env: 'EUREKA_CLIENT_INSTANCEINFOREPLICATIONINTERVALSECONDS',
key: 'heartbeatInterval',
unit: 'seconds',
},
// Future: add entries here for circuit breaker properties (#4775)
// { env: 'EUREKA_CLIENT_MAXFAILURES', key: 'maxFailures', unit: 'milliseconds' },
// { env: 'EUREKA_CLIENT_COOLDOWNTIME', key: 'cooldownTime', unit: 'seconds' },
// { env: 'EUREKA_CLIENT_BACKOFFMAX', key: 'backoffMax', unit: 'seconds' },
Comment thread
balhar-jakub marked this conversation as resolved.
Outdated
];

function parsePositiveInt(envName) {
const raw = process.env[envName];
if (raw === undefined || raw === '') return undefined;
const parsed = parseInt(raw, 10);
if (isNaN(parsed) || parsed <= 0) {
const msg = `Invalid value for ${envName}: "${raw}". `
+ 'Expected a positive integer. Using default.';
logger.warn(msg);
return undefined;
}
return parsed;
}

export default function envConfig() {
const result = { eureka: {} };
for (const { env, key, unit } of ENV_MAP) {
const value = parsePositiveInt(env);
if (value !== undefined) {
result.eureka[key] = unit === 'seconds' ? value * 1000 : value;
}
}
return result;
}
2 changes: 1 addition & 1 deletion onboarding-enabler-nodejs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function init() {
};
} else {
throw new Error(
'Invalid TLS configuration: provide either p12File or certificate + keystore'
'Invalid TLS configuration: provide either p12File or certificate + keystore'
);
}

Expand Down
54 changes: 54 additions & 0 deletions onboarding-enabler-nodejs/test/EurekaClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1420,4 +1420,58 @@ describe('Eureka client', () => {
expect(client.cache.app.THEAPP).to.have.length(0);
});
});

describe('env var configuration', () => {
const REGISTRY_ENV = 'EUREKA_CLIENT_REGISTRYFETCHINTERVALSECONDS';
const HEARTBEAT_ENV = 'EUREKA_CLIENT_INSTANCEINFOREPLICATIONINTERVALSECONDS';

afterEach(() => {
delete process.env[REGISTRY_ENV];
delete process.env[HEARTBEAT_ENV];
});

it('should override registryFetchInterval from env var', () => {
process.env[REGISTRY_ENV] = '60';
const client = new Eureka(makeConfig());
expect(client.config.eureka.registryFetchInterval).to.equal(60000);
});

it('should override heartbeatInterval from env var', () => {
process.env[HEARTBEAT_ENV] = '45';
const client = new Eureka(makeConfig());
expect(client.config.eureka.heartbeatInterval).to.equal(45000);
});

it('should let constructor config override env var', () => {
process.env[REGISTRY_ENV] = '60';
const client = new Eureka(makeConfig({
eureka: { registryFetchInterval: 9999 },
}));
expect(client.config.eureka.registryFetchInterval).to.equal(9999);
});

it('should use default values when env var is not set', () => {
const client = new Eureka(makeConfig());
expect(client.config.eureka.registryFetchInterval).to.equal(30000);
expect(client.config.eureka.heartbeatInterval).to.equal(30000);
});

it('should fall back to default on non-numeric env var', () => {
process.env[REGISTRY_ENV] = 'sixty';
sinon.spy(console, 'warn');
const client = new Eureka(makeConfig());
expect(client.config.eureka.registryFetchInterval).to.equal(30000);
expect(console.warn).to.have.been.calledWith(sinon.match('Invalid value for EUREKA_CLIENT_REGISTRYFETCHINTERVALSECONDS'));
console.warn.restore();
});

it('should fall back to default on zero env var', () => {
process.env[REGISTRY_ENV] = '0';
sinon.spy(console, 'warn');
const client = new Eureka(makeConfig());
expect(client.config.eureka.registryFetchInterval).to.equal(30000);
expect(console.warn).to.have.been.calledWith(sinon.match('Invalid value for EUREKA_CLIENT_REGISTRYFETCHINTERVALSECONDS'));
console.warn.restore();
});
});
});
Loading