feat: Add IP_RANGES_FETCH_ENABLED environment variable

This change adds a new environment variable to control whether IP ranges
are fetched during application startup. When set to 'false', the initial
fetch will be skipped, which can:

1. Speed up application startup
2. Avoid connectivity issues in environments with restricted internet access
3. Prevent startup failures when CloudFront or CloudFlare services are unreachable
This commit is contained in:
cg-zhou
2025-02-26 19:25:50 +08:00
parent 79d28f03d0
commit 1c47fc2ba4
2 changed files with 22 additions and 1 deletions

View File

@ -3,6 +3,8 @@
const schema = require('./schema');
const logger = require('./logger').global;
const IP_RANGES_FETCH_ENABLED = process.env.IP_RANGES_FETCH_ENABLED !== 'false';
async function appStart () {
const migrate = require('./migrate');
const setup = require('./setup');
@ -13,7 +15,18 @@ async function appStart () {
return migrate.latest()
.then(setup)
.then(schema.getCompiledSchema)
.then(internalIpRanges.fetch)
.then(() => {
if (IP_RANGES_FETCH_ENABLED) {
logger.info('IP Ranges fetch is enabled');
return internalIpRanges.fetch().catch(err => {
logger.error('IP Ranges fetch failed, continuing anyway:', err.message);
return Promise.resolve();
});
} else {
logger.info('IP Ranges fetch is disabled by environment variable');
return Promise.resolve();
}
})
.then(() => {
internalCertificate.initTimer();
internalIpRanges.initTimer();