From b36fb57a8da7036b4f03fe5d0726c223204d28a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Czerpak?= <89903002+lukaszczerpak-cloudinary@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:20:31 +0100 Subject: [PATCH] - changed name of WTP_LS_ALLOW_REGEX to WTP_LS_ALLOWED_LOCATIONS_REGEX (#100) - introduced a new option WTP_LS_ENABLED --- config/default.js | 3 ++- wtp/locationSelector.js | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/default.js b/config/default.js index bd3ecad..4f89731 100644 --- a/config/default.js +++ b/config/default.js @@ -37,7 +37,8 @@ const conf = { }, timeout: process.env.WTP_TIMEOUT || 30000, "locationSelector": { - "allowRegex": process.env.WTP_LS_ALLOW_REGEX || '_US_', + "enabled": process.env.WTP_LS_ENABLED || false, + "allowedLocationsRegex": process.env.WTP_LS_ALLOWED_LOCATIONS_REGEX || '_US_', "cacheTtl": process.env.WTP_LS_CACHE_TTL || 10, "updateTimeout": process.env.WTP_LS_UPDATE_TIMEOUT || 20, "defaultLocation": process.env.WTP_LS_DEFAULT_LOCATION || "IAD_US_01" diff --git a/wtp/locationSelector.js b/wtp/locationSelector.js index 07ec370..0eed3ab 100644 --- a/wtp/locationSelector.js +++ b/wtp/locationSelector.js @@ -29,9 +29,10 @@ const locationMetrics = { class LocationSelector { constructor() { if (!LocationSelector.instance) { + this.enabled = config.get('wtp.locationSelector.enabled'); this.cachedAllLocations = []; this.location = config.get('wtp.locationSelector.defaultLocation'); - this.allowRegex = new RegExp(config.get('wtp.locationSelector.allowRegex')); + this.allowedLocationsRegex = new RegExp(config.get('wtp.locationSelector.allowedLocationsRegex')); this.lastUpdated = null; this.mutex = withTimeout(new Mutex(), config.get('wtp.locationSelector.updateTimeout') * 1000); LocationSelector.instance = this; @@ -114,7 +115,7 @@ class LocationSelector { } const filtered = Object.keys(newLocations) - .filter(key => this.allowRegex.test(key)) + .filter(key => this.allowedLocationsRegex.test(key)) .reduce((arr, key) => { return [...arr, newLocations[key]]; }, []); @@ -154,7 +155,7 @@ class LocationSelector { }; async getLocation() { - if (this.isExpired()) { + if (this.enabled && this.isExpired()) { try { await this.mutex.runExclusive(async () => { if (this.isExpired()) {