Authored by 周少峰

Merge branch 'hotfix/limit'

@@ -15,7 +15,7 @@ const cachePage = { @@ -15,7 +15,7 @@ const cachePage = {
15 '/lifestyle/': 30 * SECOND, 15 '/lifestyle/': 30 * SECOND,
16 16
17 // 商品详情页 17 // 商品详情页
18 - '/product/\\/([\\d]+)(.*)/': 5 * MINUTE, 18 + '/product/^\\/([\\d]+)(.*)/': 5 * MINUTE,
19 19
20 // 逛 20 // 逛
21 '/guang/': 1 * MINUTE, 21 '/guang/': 1 * MINUTE,
@@ -30,7 +30,9 @@ module.exports = (limiter, policy) => { @@ -30,7 +30,9 @@ module.exports = (limiter, policy) => {
30 next = limiter.next; // eslint-disable-line 30 next = limiter.next; // eslint-disable-line
31 31
32 const key = `pc:limiter:${limiter.remoteIp}`; 32 const key = `pc:limiter:${limiter.remoteIp}`;
  33 + const keyMax = `pc:limiter:max:${limiter.remoteIp}`;
33 const key10m = `pc:limiter:10m:${limiter.remoteIp}`; 34 const key10m = `pc:limiter:10m:${limiter.remoteIp}`;
  35 + const key10mMax = `pc:limiter:10m:max:${limiter.remoteIp}`;
34 36
35 res.on('render', function() { 37 res.on('render', function() {
36 let route = req.route ? req.route.path : ''; 38 let route = req.route ? req.route.path : '';
@@ -49,13 +51,18 @@ module.exports = (limiter, policy) => { @@ -49,13 +51,18 @@ module.exports = (limiter, policy) => {
49 } 51 }
50 }); 52 });
51 53
52 - return cache.getMultiAsync([key, key10m]).then((results) => { 54 + return cache.getMultiAsync([key, key10m, keyMax, key10mMax]).then((results) => {
53 let result = results[key]; 55 let result = results[key];
54 let result10m = results[key10m]; 56 let result10m = results[key10m];
55 57
56 logger.debug('qps limiter: ' + key + '@' + result + ' max: ' + MAX_QPS); 58 logger.debug('qps limiter: ' + key + '@' + result + ' max: ' + MAX_QPS);
57 logger.debug('qps limiter:10m ' + key10m + '@' + result10m + ' max: ' + MAX_QPS_10m); // eslint-disable-line 59 logger.debug('qps limiter:10m ' + key10m + '@' + result10m + ' max: ' + MAX_QPS_10m); // eslint-disable-line
58 60
  61 + // 达到1分钟或是10分钟的访问限制,禁止访问
  62 + if (results[keyMax] === 1 || results[key10mMax] === 1) {
  63 + return Promise.resolve(policy);
  64 + }
  65 +
59 // 默认数据设置 66 // 默认数据设置
60 if (!result && !_.isNumber(result)) { 67 if (!result && !_.isNumber(result)) {
61 cache.setAsync(key, 1, 60); // 设置key,1m失效 68 cache.setAsync(key, 1, 60); // 设置key,1m失效
@@ -79,7 +86,7 @@ module.exports = (limiter, policy) => { @@ -79,7 +86,7 @@ module.exports = (limiter, policy) => {
79 res.statusCode = 403; 86 res.statusCode = 403;
80 return Promise.resolve(policy); 87 return Promise.resolve(policy);
81 } else if (result10m > MAX_QPS_10m) { // eslint-disable-line 88 } else if (result10m > MAX_QPS_10m) { // eslint-disable-line
82 - cache.touch(key10m, ONE_DAY); 89 + cache.setAsync(key10mMax, 1, ONE_DAY);
83 logger.debug('req limit', key10m); 90 logger.debug('req limit', key10m);
84 91
85 return Promise.resolve(policy); 92 return Promise.resolve(policy);
@@ -90,7 +97,7 @@ module.exports = (limiter, policy) => { @@ -90,7 +97,7 @@ module.exports = (limiter, policy) => {
90 res.statusCode = 403; 97 res.statusCode = 403;
91 return Promise.resolve(policy); 98 return Promise.resolve(policy);
92 } else if (result > MAX_QPS) { // 判断 qps 99 } else if (result > MAX_QPS) { // 判断 qps
93 - cache.touch(key, ONE_DAY); 100 + cache.setAsync(keyMax, 1, ONE_DAY);
94 logger.debug('req limit', key); 101 logger.debug('req limit', key);
95 102
96 return Promise.resolve(policy); 103 return Promise.resolve(policy);