Authored by yyq

fix limit

... ... @@ -61,8 +61,7 @@ const submitValidate = {
remoteIp = arr[0];
}
// pc:limiter:IP 和PC端共用
let operations = [cache.delAsync(`pc:limiter:${remoteIp}`)];
let operations = [cache.delAsync(`${config.app}:limiter:${remoteIp}`)];
// 验证码之后一小时之内不再限制qps
if (req.session.apiLimitValidate || req.session.apiRiskValidate) {
... ...
'use strict';
const cache = global.yoho.cache.master;
const _ = require('lodash');
const logger = global.yoho.logger;
const config = global.yoho.config;
module.exports = (limiter, policy) => {
// 和pc共用
const key = `pc:limiter:${limiter.remoteIp}`;
const ipBlackKey = `pc:limiter:${limiter.remoteIp}`; // ci ip黑名单
const ipLimitKey = `${config.app}:limiter:${limiter.remoteIp}`; // 业务黑名单
return cache.getAsync(key).then((result) => {
logger.debug(key, result);
return Promise.all([
cache.getAsync(ipBlackKey),
cache.getAsync(ipLimitKey)
]).then(result => {
let ipBlackRes = result[0];
let ipLimitRes = result[1];
if (result && _.isNumber(result) && result !== -1) {
logger.debug(ipBlackKey, ipBlackRes);
logger.debug(ipLimitKey, ipLimitRes);
if ((ipBlackRes && +ipBlackRes > 0) || (ipLimitRes && +ipLimitRes > 0)) {
return Promise.resolve(policy);
} else {
return Promise.resolve(true);
... ...
... ... @@ -45,10 +45,10 @@ module.exports = (limiter, policy) => {
if (!results[key]) {
operation.push(cache.setAsync(cacheKey, 1, +key));
} else if (+results[key] > +val) {
logger.warn(`pc:limiter:${limiter.remoteIp}`);
logger.warn(`${config.app}:limiter:${limiter.remoteIp}`);
// ip限制1小时
operation.push(cache.setAsync(`pc:limiter:${limiter.remoteIp}`, 1, config.LIMITER_IP_TIME));
operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, config.LIMITER_IP_TIME));
return Promise.resolve(policy);
} else {
operation.push(cache.incrAsync(cacheKey, 1));
... ...