Skip to content

Commit 5c26475

Browse files
make location selector attributes configurable
1 parent 4ec766a commit 5c26475

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

config/default.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ const conf = {
3535
"lcp": process.env.LCP_PATH || "data.median.firstView.largestPaints",
3636
"lcpURL": process.env.LCP_URL_PATH || "data.median.firstView.LargestContentfulPaintImageURL"
3737
},
38-
timeout: process.env.WTP_TIMEOUT || 30000
38+
timeout: process.env.WTP_TIMEOUT || 30000,
39+
"locationSelector": {
40+
"cacheTtl": process.env.WTP_LS_CACHE_TTL || 10,
41+
"updateTimeout": process.env.WTP_LS_UPDATE_TIMEOUT || 20,
42+
"defaultLocation": process.env.WTP_LS_DEFAULT_LOCATION || "IAD_US_01"
43+
}
3944
},
4045
"cloudinary": {
4146
"cloudName": process.env.CLOUDINARY_NAME,

wtp/locationSelector.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const got = (...args) => import('got').then(({default: got}) => got(...args));
2+
const config = require('config');
23
const {Mutex, withTimeout, E_TIMEOUT} = require('async-mutex');
34
const apiKeys = require('./apiKey');
45
const path = require("path");
@@ -7,24 +8,20 @@ const logger = require('../logger').logger;
78
const GET_LOCATIONS = 'http://www.webpagetest.org/getLocations.php?f=json';
89

910
class LocationSelector {
10-
CACHE_TTL = 10;
11-
DEFAULT_LOCATION = 'IAD_US_01';
12-
UPDATE_LOCATIONS_TIMEOUT = 20;
13-
1411
constructor() {
1512
if (!LocationSelector.instance) {
1613
this.cachedAllLocations = [];
17-
this.location = this.DEFAULT_LOCATION;
14+
this.location = config.get('locationSelector.locationSelector.defaultLocation');
1815
this.lastUpdated = null;
19-
this.mutex = withTimeout(new Mutex(), this.UPDATE_LOCATIONS_TIMEOUT * 1000);
16+
this.mutex = withTimeout(new Mutex(), config.get('locationSelector.locationSelector.updateTimeout') * 1000);
2017
LocationSelector.instance = this;
2118
}
2219
return LocationSelector.instance;
2320
}
2421

2522
isExpired() {
2623
const now = Date.now();
27-
return (!this.lastUpdated || (now - this.lastUpdated) > this.CACHE_TTL * 1000);
24+
return (!this.lastUpdated || (now - this.lastUpdated) > config.get('locationSelector.locationSelector.cacheTtl') * 1000);
2825
}
2926

3027
async fetchLocations() {
@@ -136,5 +133,4 @@ class LocationSelector {
136133
}
137134

138135
const instance = new LocationSelector();
139-
140136
module.exports = instance;

0 commit comments

Comments
 (0)