Egg: 请问eggjs 有没有API 接口访问频率控制的插件?

Created on 15 Apr 2018  ·  7Comments  ·  Source: eggjs/egg

需要对API接口访问频率进行控制,比如每分钟最多20次,请问eggjs 有没有API 接口访问频率控制的插件?

  • Node Version: 8.9.4
  • Egg Version: 2.3
  • Plugin Name:
  • Plugin Version:
  • Platform: Linux
  • Mini Showcase Repository:

Most helpful comment

解决了,分享一下方案:

// app/middleware/ratelimit.js
module.exports = require('koa-ratelimit')
// config/config.default.js

  config.middleware = [  'ratelimit' ]

  config.ratelimit = {
    db: new Redis(),
    duration: 60000,
    errorMessage: '您访问的频率太高了,请手下留情!',
    id: (ctx) => ctx.ip,
    headers: {
      remaining: 'Rate-Limit-Remaining',
      reset: 'Rate-Limit-Reset',
      total: 'Rate-Limit-Total'
    },
    max: 5,
    disableHeader: true,
  }

All 7 comments

Translation of this issue:


Does eggjs have an API interface to access frequency control plugins?

Need to control the API interface access frequency, such as no minutes up to 20 times, does eggjs have API interface to access frequency control plug-ins?

  • Node Version:
  • Egg Version:
  • Plugin Name:
  • Plugin Version:
  • Platform:
  • Mini Showcase Repository:

解决了,分享一下方案:

// app/middleware/ratelimit.js
module.exports = require('koa-ratelimit')
// config/config.default.js

  config.middleware = [  'ratelimit' ]

  config.ratelimit = {
    db: new Redis(),
    duration: 60000,
    errorMessage: '您访问的频率太高了,请手下留情!',
    id: (ctx) => ctx.ip,
    headers: {
      remaining: 'Rate-Limit-Remaining',
      reset: 'Rate-Limit-Reset',
      total: 'Rate-Limit-Total'
    },
    max: 5,
    disableHeader: true,
  }

@aka99 请教下Redis这块是ioredis这个模块吗,因为是配置文件里面,所以你这个实例化Redis的操作怎么处理?

@aka99 egg-ratelimiter

egg-ratelimiter 只能用redis?不能用其他吗?

egg-ratelimiter 只能用redis?不能用其他吗?

egg-ratelimiter 基于ratelimiter,而ratelimiter是用内存其实(就是应用程序的变量)或者redis来处理,
推荐用下面这个,有好几种数据库的选择
https://www.npmjs.com/package/koa2-ratelimit

egg-ratelimiter 只能用redis?不能用其他吗?

egg-ratelimiter 基于ratelimiter,而ratelimiter是用内存其实(就是应用程序的变量)或者redis来处理,
推荐用下面这个,有好几种数据库的选择
https://www.npmjs.com/package/koa2-ratelimit
感谢大佬 这么快就回复了。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dizhifeng picture dizhifeng  ·  3Comments

lvgg3271 picture lvgg3271  ·  3Comments

popomore picture popomore  ·  3Comments

kylezhang picture kylezhang  ·  3Comments

bupafengyu picture bupafengyu  ·  3Comments