path-white-list.js
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const _ = require('lodash');
const logger = global.yoho.logger;
const cache = global.yoho.cache.master;
const WHITE_LIST_KEY = 'pc:limiter:whitelist:path';
const DEFAULT_PATH_WHITE_LIST = [
'/3party/check',
'/passport/images.png',
'/passport/cert/headerTip',
'/common/getbanner',
'/common/suggestfeedback',
'/product/search/history',
'/product/search/suggest'
];
const cacheWhiteList = {
nowTime() {
return Date.parse(new Date()) / 1000;
},
getValue() {
if (this.updateTime || this.nowTime() - this.updateTime > 60 * 10) {
this.syncRemoteConfig();
}
return _.uniq(_.concat([], DEFAULT_PATH_WHITE_LIST, _.toArray(this.whiteList)));
},
syncRemoteConfig() {
if (this.syncing) {
return;
}
this.syncing = true;
cache.getAsync(WHITE_LIST_KEY).then(res => {
this.updateTime = this.nowTime();
if (!res) {
return;
}
this.whiteList = JSON.parse(res);
this.syncing = false;
}).catch((e) => {
this.syncing = false;
logger.debug('whitelist path parse error. ' + JSON.stringfy(e));
});
}
};
module.exports = () => {
return cacheWhiteList.getValue();
};