Authored by 郝肖肖

'pc:limiter:IP共用'

... ... @@ -70,7 +70,7 @@ module.exports = (req, res, next) => {
// 排除条件:ip白名单/路径白名单/异步请求/登录用户
const excluded = _.includes(IP_WHITE_LIST, remoteIp) ||
_.includes(PATH_WHITE_LIST, req.path) || req.xhr || !_.isEmpty(_.get(req, 'user.uid'));
const enabled = !_.get(req.app.locals, 'pc.sys.noLimiter');
const enabled = !_.get(req.app.locals, 'wap.sys.noLimiter');
logger.debug(`request remote ip: ${remoteIp}; excluded: ${excluded}; enabled: ${enabled}`);
... ...
... ... @@ -2,16 +2,16 @@
const cache = global.yoho.cache.master;
const _ = require('lodash');
const config = global.yoho.config;
const logger = global.yoho.logger;
module.exports = (limiter, policy) => {
const key = `${config.app}:limiter:${limiter.remoteIp}`;
// 和pc共用
const key = `pc:limiter:${limiter.remoteIp}`;
return cache.getAsync(key).then((result) => {
logger.debug(key, result);
if (result && _.isNumber(result)) {
if (result && _.isNumber(result) && result !== -1) {
return Promise.resolve(policy);
} else {
return Promise.resolve(true);
... ...
... ... @@ -48,10 +48,10 @@ module.exports = (limiter, policy) => {
if (!results[key]) {
operation.push(cache.setAsync(cacheKey, 1, +key));
} else if (+results[key] > +val) {
logger.warn(`${config.app}:limiter:${limiter.remoteIp}`);
logger.warn(`pc:limiter:${limiter.remoteIp}`);
// ip限制1小时
operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime));
operation.push(cache.setAsync(`pc:limiter:${limiter.remoteIp}`, 1, limiterIpTime));
return Promise.resolve(policy);
} else {
operation.push(cache.incrAsync(cacheKey, 1));
... ...