qps-limit2.js 972 Bytes
/**
 * 限制页面访问次数,如超过限制次数,返回相应策略(目前是ip加入黑名单,跳转图形验证码页面,解除限制)
 * 当前规则只针对未登录用户
 */

const logger = global.yoho.logger;
const cache = global.yoho.cache.master;
const config = global.yoho.config;

const limitKey = 'limit2';

module.exports = (limiter, policy) => {
    let getOp = {};

    getOp.human = cache.getAsync(`${config.app}:${limitKey}:ishuman:${limiter.remoteIp}`);
    getOp.limit = cache.getAsync(`${config.app}:${limitKey}:${limiter.remoteIp}`);

    return Promise.props(getOp).then((results) => {
        console.log('enable ===>', results);
        if (results.human) {
            return Promise.resolve(true);
        }

        if (results.limit) {
            return Promise.resolve(policy);
        }

        return Promise.resolve(true);
    }).catch(err=>{
        logger.error(err);

        return Promise.resolve(true);
    });
};