Showing
1 changed file
with
5 additions
and
2 deletions
@@ -20,7 +20,8 @@ module.exports = (limiter, policy) => { | @@ -20,7 +20,8 @@ module.exports = (limiter, policy) => { | ||
20 | 20 | ||
21 | // 存储规则的cache keys | 21 | // 存储规则的cache keys |
22 | let ruleKeys = {}; | 22 | let ruleKeys = {}; |
23 | - let getOp = {} | 23 | + let getOp = {}; |
24 | + | ||
24 | _.forEach(MAX_TIMES, (val, key) => { | 25 | _.forEach(MAX_TIMES, (val, key) => { |
25 | ruleKeys[key] = `${config.app}:limiter:${key}:max:${limiter.remoteIp}`; | 26 | ruleKeys[key] = `${config.app}:limiter:${key}:max:${limiter.remoteIp}`; |
26 | getOp[key] = cache.getAsync(ruleKeys[key]); | 27 | getOp[key] = cache.getAsync(ruleKeys[key]); |
@@ -34,12 +35,13 @@ module.exports = (limiter, policy) => { | @@ -34,12 +35,13 @@ module.exports = (limiter, policy) => { | ||
34 | 35 | ||
35 | // 遍历限制规则,若满足返回相应处理策略, 否则页面访问次数加1 | 36 | // 遍历限制规则,若满足返回相应处理策略, 否则页面访问次数加1 |
36 | let operation = []; | 37 | let operation = []; |
38 | + | ||
37 | _.forEach(MAX_TIMES, (val, key) => { | 39 | _.forEach(MAX_TIMES, (val, key) => { |
38 | let cacheKey = ruleKeys[key]; | 40 | let cacheKey = ruleKeys[key]; |
39 | 41 | ||
40 | if (!results[key]) { | 42 | if (!results[key]) { |
41 | operation.push(cache.setAsync(cacheKey, 1, +key)); | 43 | operation.push(cache.setAsync(cacheKey, 1, +key)); |
42 | - } else if (+results[cacheKey] > +val) { | 44 | + } else if (+results[key] > +val) { |
43 | 45 | ||
44 | // ip限制1小时 | 46 | // ip限制1小时 |
45 | operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime)); | 47 | operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime)); |
@@ -50,6 +52,7 @@ module.exports = (limiter, policy) => { | @@ -50,6 +52,7 @@ module.exports = (limiter, policy) => { | ||
50 | }); | 52 | }); |
51 | 53 | ||
52 | Promise.all(operation); | 54 | Promise.all(operation); |
55 | + | ||
53 | // 不满足任何限制规则,继续访问 | 56 | // 不满足任何限制规则,继续访问 |
54 | return Promise.resolve(true); | 57 | return Promise.resolve(true); |
55 | }).catch(err=>{ | 58 | }).catch(err=>{ |
-
Please register or login to post a comment