速率限制 GET 请求 [英] Rate limit GET requests

查看:63
本文介绍了速率限制 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部 API,可将 API 请求的速率限制为每秒最多 25 个请求.我想将部分结果插入到 MongoDB 数据库中.

I have an external API that rate-limits API requests to up to 25 requests per second. I want to insert parts of the results into a MongoDB database.

如何限制请求函数的速率,以免错过所有数组的任何 API 结果?

How can I rate limit the request function so that I don't miss any of API results for all of the array?

MongoClient.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
    if (err) {
        throw err;
    } else {
        for (var i = 0; i < arr.length; i++) {
            //need to rate limit the following function, without missing any value in the arr array
            request( {
                method: 'GET',
                url: 'https://SOME_API/json?address='+arr[i]
            },
            function (error, response, body) {
                //doing computation, including inserting to mongo
            }
            )
        };
    }
});

推荐答案

这可能可以使用 request-rate-limiter 包来完成.因此,您可以将其添加到您的代码中:

This could possibly be done using the request-rate-limiter package. So you can add this to your code :

var RateLimiter = require('request-rate-limiter');
const REQS_PER_MIN = 25 * 60; // that's 25 per second
var limiter = new RateLimiter(REQS_PER_MIN);

并且由于 request-rate-limiter 基于 request 你可以用 limiter.request 替换 request>

and since request-rate-limiter is based on request you can just replace request with limiter.request

您可以在包的 npm 页面上找到更多信息 - https://www.npmjs.com/package/request-rate-limiter

You can find further information on the package's npm page - https://www.npmjs.com/package/request-rate-limiter

就个人而言 - 我会用承诺替换所有这些回调

这篇关于速率限制 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆