Skip to content

Commit

Permalink
- changed name of WTP_LS_ALLOW_REGEX to WTP_LS_ALLOWED_LOCATIONS_REGEX (
Browse files Browse the repository at this point in the history
#100)

- introduced a new option WTP_LS_ENABLED
  • Loading branch information
lukaszczerpak-cloudinary authored Dec 10, 2024
1 parent 4768b7c commit b36fb57
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions wtp/locationSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]];
}, []);
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit b36fb57

Please sign in to comment.