简体   繁体   English

使用Supertest限制每分钟的请求数

[英]Limit number of requests per minute using Supertest

We're using supertest with Typescript to test out APIs. 我们正在对Typescript使用Typescript来测试API。 For some of them (eg user registration, change password, etc) an email address is sent that is required for confirmation (user confirm token, reset password token, etc). 对于其中的某些(例如,用户注册,更改密码等),将发送确认所需的电子邮件地址(用户确认令牌,重置密码令牌等)。

In order to achieve this, we decided to use GuerillaMail , as it's a simple disposable email client with API. 为了实现这一目标,我们决定使用GuerillaMail ,因为它是带有API的简单一次性电子邮件客户端。 After doing the prerequisites (setting the email using their email), the following piece of code does its job in a couple of cases: 在完成先决条件(使用电子邮件设置电子邮件)之后,以下代码在两种情况下起作用:

private async getEmailId(sid_token: string, emailType: EmailType): Promise<string> {
        var mail;
        var mailToken = this.getSidToken(sid_token);

        //Keep trying until the email gets in the inbox
        // Infinite loop is prevented by the jest framework timeout
        while (!mail) {
            const result = await request(this.guerillaMailApiUrl)
                .get('')
                .query({
                    f: 'check_email',
                    seq: 0,
                    sid_token: mailToken
                });
            if (result.body.list != undefined) {
                mail = result.body.list.filter(m => m.mail_subject == emailType && m.mail_from == 'email@domain.com' && m.mail_read == 0)[0];
            }
            else {
                mail = undefined;
            }
        }
        return mail.mail_id;
    }

However, it comes with a limitation of 20 requests per minute, limitation that is causing tests to fail. 但是,它的限制是每分钟20个请求,该限制导致测试失败。

Is there a way to limit the number of request made? 有没有办法限制请求的数量?

LATER EDIT: I made it work by creating a delay: 稍后编辑:我通过创建延迟使其起作用:

async delay(ms: number) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

and calling it right before exiting the while loop: 并在退出while循环之前立即调用它:

await this.delay(5000);

Is there a cleaner/nicer/efficient/performant/etc way of achieving this? 是否有一种更清洁/更精细/效率更高/性能更高的方式来实现这一目标?

This one rate limiter that I used in my past projects Bottleneck https://www.npmjs.com/package/bottleneck 我在过去的项目瓶颈中使用过的一种速率限制器https://www.npmjs.com/package/bottleneck

const limiter = new Bottleneck({
  maxConcurrent: 20,
  minTime: 60000
});

while (!mail) {
  // set limiter here
  const result = await limiter.schedule(() => request(this.guerillaMailApiUrl)
    .get('')
    .query({
      f: 'check_email',
      seq: 0,
      sid_token: mailToken
    }));
  if (result.body.list != undefined) {
    mail = result.body.list.filter(m => m.mail_subject == emailType && m.mail_from == 'email@domain.com' && m.mail_read == 0)[0];
  } else {
    mail = undefined;
  }
}

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM