首页 > 解决方案 > 此包中的 ratelimit 装饰器 loopback4-ratelimiter 不起作用

问题描述

此包loopback4-ratelimiter中的ratelimit 装饰器不起作用(没有错误)。使用序列它工作正常,但我想限制特定 api 的速率。我已将其从序列中删除。
我正在使用一个名为“redis”的新创建的 Redis 数据源。当通过序列实现速率限制时,我可以看到在 redis 中创建了键。

下面是我的实现:

控制器.ts

  @ratelimit(true, {
      max: 4
  })
  @post('/add_user', {
    responses: {
      '200': {
        description: 'Add User',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                is_success: {
                  type: 'boolean',
                },
              },
            },
          },
        },
      },
    },
  })
  async addUser(): Promise<{is_success: boolean}> {
      return {is_success: true}
  }

应用程序.ts

    this.component(RateLimiterComponent);
    this.bind(RateLimitSecurityBindings.CONFIG).to({
      name: 'redis',
      max: 10,
      windowMs: 900000, // #9 
      message: 'Too many requests, please try again later.'
    });

包.json

  "engines": {
      "node": ">=10.16"
  },
  "dependencies": {
      "@loopback/authentication": "^7.0.1",
      "@loopback/authentication-jwt": "^0.7.1",
      "@loopback/authorization": "0.7.1",
      "@loopback/boot": "^3.0.1",
      "@loopback/core": "^2.10.1",
      "@loopback/cron": "^0.4.1",
      "@loopback/repository": "^3.0.1",
      "@loopback/rest": "^7.0.1",
      "@loopback/rest-explorer": "^3.0.1",
      "@loopback/service-proxy": "^3.0.1",
      "loopback-connector-kv-redis": "3.0.0",
      "loopback4-ratelimiter": "^2.2.0",
   }
   "devDependencies": {
      "@loopback/build": "^6.2.4",
      "@types/node": "^10.17.35",
      "typescript": "~4.0.2"
    }

GitHub问题 - https://github.com/sourcefuse/loopback4-ratelimiter/issues/19#issue-890804261

标签: node.jstypescriptexpressloopbackjsloopback4

解决方案


推荐阅读