Authored by 周少峰

hotfix/limit

... ... @@ -3,6 +3,7 @@
const cache = global.yoho.cache.master;
const Promise = require('bluebird');
const co = Promise.coroutine;
const config = global.yoho.config;
const HeaderModel = require('../../../doraemon/models/header');
... ... @@ -15,23 +16,9 @@ const index = co(function* (channel) {
});
const removeBlack = (remoteIp) => {
let key = `pc:limiter:${remoteIp}`,
key10m = `pc:limiter:10m:${remoteIp}`,
keyMax = `pc:limiter:max:${remoteIp}`,
key10mMax = `pc:limiter:10m:max:${remoteIp}`,
synchronizeKey = `pc:limiter:synchronize:${remoteIp}`,
asynchronousKey = `pc:limiter:asynchronous:${remoteIp}`,
spiderKey = `pc:limiter:spider:${remoteIp}`;
let key = `${config.app}:limiter:${remoteIp}`;
return Promise.all([
cache.delAsync(key),
cache.delAsync(key10m),
cache.delAsync(keyMax),
cache.delAsync(key10mMax),
cache.delAsync(synchronizeKey),
cache.delAsync(asynchronousKey),
cache.delAsync(spiderKey)
cache.delAsync(key)
]);
};
... ...
... ... @@ -2,15 +2,14 @@
const cache = global.yoho.cache.master;
const _ = require('lodash');
const config = global.yoho.config;
module.exports = (limiter) => {
const key = `pc:limiter:${limiter.remoteIp}`;
module.exports = (limiter, policy) => {
const key = `${config.app}:limiter:${limiter.remoteIp}`;
return cache.getAsync(key).then((result) => {
if (result && _.isNumber(result)) {
return Promise.resolve({
exclusion: result === -1
});
return Promise.resolve(policy);
} else {
return Promise.resolve(true);
}
... ...
... ... @@ -10,6 +10,9 @@ const cache = global.yoho.cache.master;
const config = global.yoho.config;
const _ = require('lodash');
// 超出访问限制ip限制访问1小时
const limiterIpTime = 3600;
// 页面访问限制
const MAX_TIMES = {
// 30s 最多访问15次
... ... @@ -57,6 +60,7 @@ module.exports = (limiter, policy) => {
if (!results[val]) {
cache.setAsync(val, 1, +cacheTime);
} else if (+results[val] > +MAX_TIMES[cacheTime]) {
cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime);
return Promise.resolve(policy);
}
... ...