如何检查使用 JS 访问端点的限制是什么

How to check what the throttling limit is for your access to an endpoint with JS

[![在此处输入图像描述][1]][1]我需要实施代码来检查端点上的节流限制(我知道它是每分钟 x 次)。我只能在 python 中找到这方面的示例,但我从未使用过。似乎我的选择是 运行 一个脚本来重复发送请求直到它限制我,或者,如果可能的话,查询 API 以查看限制是什么。

有人知道如何处理吗?

谢谢。

注意:空白 space 只是来自 api 调用的数据。 [1]: https://i.stack.imgur.com/gAFQQ.png

这开始 concurency 数量的工人(我在这里使用工人作为一个宽松的术语;不要@我)。每个人都发出尽可能多的请求,直到其中一个请求 rate-limited 或超时。他们报告在给定时间内成功完成了多少请求 window。

如果您知道 rate-limit window(根据您的问题需要 1 分钟),这将找到 rate-limit。如果您需要发现 window,您可能希望有意用尽限制,然后放慢请求并测量时间,直到它们再次开始通过。提供的代码不会执行此操作。

// call apiCall() a bunch of times, stopping when a apiCall() resolves
// false or when "until" time is reached, whichever comes first. For example
// if your limit is 50 req/min (and you give "until" enough time to
// actuially complete 50+ requests) this will call apiCall() 50 times. Each
// call should return a promise resolving to TRUE, so it will be counted as
// a success. On the 51st call you will presumably hit the limit, the API
// will return an error, apiCall() will detect that, and resolve to false.
// This will cause the worker to stop making requests and return 50.
async function workerThread(apiCall, until) {
    let successfullRequests = 0;
    while(true) {
        const success = await apiCall();
        // only count it if the request was successfull
        // AND finished within the timeframe
        if(success && Date.now() < until) {
            successfullRequests++;
        } else {
            break;
        }
    }
    return successfullRequests;
}

// this just runs a bunch of workerThreads in parallell, since by doing a
// single request at a time, you might not be able to hit the limit
// depending on how slow the API is to return. It returns the sum of each
// workerThread(), AKA the total number of apiCall()s that resolved to TRUE
// across all threads.
async function testLimit(apiCall, concurency, time) {
    const endTime = Date.now() + time;
    
    // launch "concurency" number of requests
    const workers = [];
    while(workers.length < concurency) {
        workers.push(workerThread(apiCall, endTime));
    }
    
    // sum the number of requests that succeded from each worker.
    // this implicitly waits for them to finish.
    let total = 0;
    for(const worker of workers) {
        total += await worker;
    }
    return total;
}

// put in your own code to make a trial API call.
// return true for success or false if you were throttled.
async function yourAPICall() {
    try {
        // this is a really sloppy example API
        // the limit is ROUGHLY 5/min, but because of the sloppy server-side
        // implimentation you might get 4-6.
        const resp = await fetch("https://9072997.com/demos/rate-limit/");
        return resp.ok;
    } catch {
        return false;
    }
}

// this is a demo of how to use the function
(async function() {
    // run 2 requests at a time for 5 seconds
    const limit = await testLimit(yourAPICall, 2, 5*1000);
    console.log("limit is " + limit + " requests in 5 seconds");
})();

请注意,此方法测量可用的配额自身。如果其他客户端或之前的请求已经用完配额,则会影响结果。