Authored by 周少峰

request limit

... ... @@ -14,11 +14,13 @@ const _ = require('lodash');
const MAX_TIMES = {
// 30s 最多访问15次
30: 15,
// 60s 最多访问15次
60: 20,
// 100s 最多访问15次
600: 100
}
};
module.exports = (limiter, policy) => {
const req = limiter.req;
... ... @@ -33,10 +35,9 @@ module.exports = (limiter, policy) => {
_.forEach(MAX_TIMES, (val, key) => {
ruleKeys.push(`${config.app}:limiter:${key}:max:${limiter.remoteIp}`); // eslint-disable-line
})
});
return cache.getMultiAsync(ruleKeys).then((results) => {
console.log(results);
// 第一次访问
if (_.isEmpty(results)) {
... ... @@ -44,7 +45,7 @@ module.exports = (limiter, policy) => {
let cacheTime = val.match(/limiter:([^:]*)?:max/i)[1];
cache.setAsync(val, 1, +cacheTime); // eslint-disable-line
})
});
return Promise.resolve(true);
}
... ... @@ -63,9 +64,11 @@ module.exports = (limiter, policy) => {
if (!req.xhr) {
cache.incrAsync(val, 1);
}
})
});
// 不满足任何限制规则,继续访问
return Promise.resolve(true);
}).catch(err=>{logger.error(err)});
}).catch(err=>{
logger.error(err);
});
};
... ...