发送多个 HTTP 请求

2024-01-07

我需要编写使用项目 ID 查找有关项目的信息的程序。

该 API 一次仅获取一项,因此我只能对每项执行一次查询。 API 仅限于五个同时请求。任何额外的结果都会给出 HTTP 429 错误。

如果有一个 JavaScript 对象,其中包含所有带有 ID 的项目

如何检索所有给定 ID 的信息而不触发同时请求限制,并且不对已看到的项目 ID 执行不必要的查询。

import fetch from "node-fetch";

let itemObject = [
  { itemName: "", itemID: "" },
  { itemName: "", itemID: "" },
  { itemName: "", itemID: "" },
  { itemName: "", itemID: "" },
];

async function sendIDRequests() {
  try {
    const response = await fetch("https://url/items/:ID", {
      headers: {
        Authorization: "",
      },
    });
    if (!response.ok) {
      throw new Error(`${response.status} ${response.statusText}`);
    }
    response
      .text()
      .then((res) => console.log(res))
      .catch((err) => {
        throw new Error(err);
      });
  } catch (error) {
    console.error(error);
  }
}

sendRequests()

为此,我想到了两种方法。批处理和滑动窗口方法。批处理可能更容易,但使用滑动窗口将是更有效的实现。

使用 Promise.all() 进行批处理

此方法创建批量请求,最多达到指定的batchSize只有当一批请求全部完成后,才会发出下一批请求。

您需要在此处添加一些错误处理,以防请求失败。

import fetch from "node-fetch";

// list of items that you might want to use to compose your URL (not actually used here)
let itemObject = [
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
];


(async () => {
    // number of concurrent requests in one batch
    const batchSize = 4;
    // request counter
    let curReq = 0;
    // as long as there are items in the list continue to form batches
    while (curReq < itemObject.length) {
        // a batch is either limited by the batch size or it is smaller than the batch size when there are less items required
        const end = itemObject.length < curReq + batchSize ? itemObject.length: curReq + batchSize;
        // we know the number of concurrent request so reserve memory for this
        const concurrentReq = new Array(batchSize);
        // issue one request for each item in the batch
        for (let index = curReq; index < end; index++) {
            concurrentReq.push(fetch("https://postman-echo.com/get"))
            console.log(`sending request ${curReq}...`)
            curReq++;
        }
        // wait until all promises are done or one promise is rejected
        await Promise.all(concurrentReq);
        console.log(`requests ${curReq - batchSize}-${curReq} done.`)
    }
})();

预期结果:

sending request 0...
sending request 1...
sending request 2...
sending request 3...
requests 0-4 done.
sending request 4...
sending request 5...
sending request 6...
sending request 7...
requests 4-8 done.
sending request 8...
sending request 9...
sending request 10...
sending request 11...
requests 8-12 done.

带信号量的滑动窗口

这种方法使用了一个滑动窗口 https://en.wikipedia.org/wiki/Sliding_window_protocol并在另一个请求完成后立即安排一个新请求,同时始终保持请求计数低于或等于最大数量n任意时刻的并发请求。 你需要实现的是信号 https://en.wikipedia.org/wiki/Semaphore_(programming).

JavaScript 中有一个用于此目的的库,名为异步互斥体 https://github.com/DirtyHairy/async-mutex.

下面是使用该库向 Postman Echo API 同时发送 2 个请求的示例程序。在信号量允许的情况下,永远不会有更多的请求同时运行(在您的情况下,限制为 5,这里是 2)。

import { Semaphore } from "async-mutex";
import fetch from "node-fetch";

// list of items that you might want to use to compose your URL (not actually used here)
let itemObject = [
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
    { itemName: "", itemID: "" },
];

(async () => {
    // allow two concurrent requests (adjust for however many are required)
    const semaphore = new Semaphore(2);

    itemObject.forEach(async (item, idx) => {
        // acquire the semaphore
        const [value, release] = await semaphore.acquire();
        // at this point the semaphore has been acquired and the job needs to be done 
        try {
            console.log(`sending request ${idx}...`)
            const response = await fetch("https://postman-echo.com/get")
            if(!response.ok){
                console.log(`request failed with status code ${response.status}`)
            }
        }
        catch (error) {
            console.log("request failed.")
        }
        finally {
            console.log(`request ${idx} done...`)
            // release the semaphore again so a new request can be issued 
            release();
        }
    })
})();

预期输出(顺序可能有所不同):

sending request 0...
sending request 1...
request 1 done...
sending request 2...
request 2 done...
sending request 3...
request 3 done...
sending request 4...
request 0 done...
sending request 5...
request 4 done...
sending request 6...
request 5 done...
sending request 7...
request 6 done...
sending request 8...
request 7 done...
sending request 9...
request 8 done...
sending request 10...
request 9 done...
sending request 11...
request 10 done...
request 11 done...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

发送多个 HTTP 请求 的相关文章

随机推荐

  • 在领域驱动设计中,您可以在 UI 中使用领域实体吗?

    在许多领先的 DDD 项目中 尤其是 MVC 风格 我看到 UI 使用镜像域实体的显示对象 而不是直接使用这些域对象 这种风格显然是为了解耦和分离关注点 我个人更喜欢这种风格 但我不确定这是否是 DDD 的严格原则 或者这是否只是不同开发人
  • 我需要做直方图拉伸

    我有一个 BitmapFrames 数组 需要进行直方图拉伸 我知道这与直方图均衡不同 最终结果是什么 有点 问题是我完全不知道得到直方图后该怎么做 到目前为止 我的代码为直方图创建了一个数组 因此我知道每个值有多少个像素 但在那之后我不知
  • 让semantic_errors呈现准确的错误消息

    我有一个模型Camping which has many Images 露营至少需要一张图片 class Camping lt ActiveRecord Base attr accessible images attributes has
  • PostgreSQL:如何实现最小基数?

    正如这个问题中的回答 PostgreSQL 中的基数 https stackoverflow com questions 3135206 cardinality in postgresql 基数是使用强制执行的限制条件 http www p
  • 使用游标获取字段值

    我正在创建一个应用程序 但遇到问题Cursor 我有一个SQLiteDatabase这给我返回了一个Cursor当我尝试使用此函数获取值时 public Cursor fetchOption long rowId throws SQLExc
  • 如何将“zd”说明符与“printf()”一起使用?

    寻求有关使用的说明 zd with printf 当然 对于 C99 及更高版本 以下内容是正确的 void print size size t sz printf zu n sz C 规范seems允许printf zd n sz 取决于
  • 如何正确对齐此文本?

    我今天写了这个极地时钟 我几乎完成了 除了我想在行内对齐我的文本 类似于this http blog pixelbreaker com polarclock 有谁知道如何做到这一点 我尝试使用 FontRenderContext 和字体指标
  • Android HttpClient 性能

    我正在开发 Android 应用程序 它使用大量对 Web 服务的 http 请求 起初 我在每个请求之前创建一个新的 HttpClient 实例 为了提高性能 我尝试在多个线程中执行请求 因此 我使用 ThreadSafeConnecti
  • 带有 c:foreach jstl 标签的二维数组列表

    我在两个叠瓦式 JSTL 中使用二维数组列表
  • 计算多个 CSV 文件中一列的平均值

    我的文件夹中有 300 多个 CSV 文件 名为 001 csv 002 csv 等 每个包含一个带有标题的数据帧 我正在编写一个函数 该函数将采用三个参数 文件的位置 要计算平均值的列的名称 数据框内 以及计算中使用的文件 这是我的功能
  • 如何在 Eclipse 中调试在 jetty 上运行的 Web 应用程序?

    以前从未做过网络编程 有没有办法设置断点 在 Eclipse 中查看变量值 我想要调试的应用程序会生成一个我想轻松提取的查询字符串 单击外部工具配置 Select program and click the new button top l
  • 无法将 React.lazy 与 Material UI 图标一起使用

    因为我的应用程序中的很多地方都有很多图标 所以我想对它们使用代码分割 我创建了辅助组件来执行此操作 import React FunctionComponent Suspense from react interface LazyMuiIc
  • 在哪里可以找到 Google Compute Engine 的名称服务器?

    我已将一个网站上传到我的 Compute Engine 实例 并且我想设置注册器以将访问者发送到服务器 Compute Engine 在哪里可以通过 Compute Engine 获取实例 服务器的名称服务器 Google Compute
  • 使用 alex/happy 与 Cabal

    我正在为我正在学习的课程编写一个编译器 该类不是专门的 Haskell 但我使用 Haskell 来编写我的编译器和解释器 我有一个 cabal 包设置 希望能让我的教授轻松运行 编译 我有快乐和亚历克斯在build tools两个可执行文
  • node.js - 配置节点将函数加载到全局范围内?

    在现实生活中 我在某个地方看到我们可以配置 Node js 在全局范围内执行加载的模块 但我现在找不到如何做到这一点 我为什么要问 我有一些遗留文件定义了我想在服务器和客户端上使用的语言实用程序 但是其中许多实用程序被定义为全局范围函数 例
  • 设计确认在首次发送时无效

    我有一个 Rails 4 应用程序 使用 Devise 3 2 2 confirmable 并且遇到了发送无效确认令牌的问题 但是只是第一次 重新发送确认令牌后 链接将起作用 相关路线 devise for users skip sessi
  • 设置信用卡号格式

    如何在输入时格式化和验证信用卡号 每 4 位数字之间留有空格 eg 4464 6846 4354 3564 我努力了 creditno keyup function cc this val split join cc cc match ne
  • 使用 SwiftUI 的relativeWidth 仅适用于框架视图

    尝试在 SwiftUI 中构建一个条形图元素 其宽度根据元素的值与其父视图成比例 这是问题的简化版本 struct BarView View var body some View Color purple relativeWidth 0 5
  • 在 DB2 中显示表的定义

    大家好 我正在学习DB2 想知道如何 创建表后查看表的特征 类似于MySQL中的EXPLAIN TABLE命令 谢谢 此外DESCRIBE TABLE 您可以使用下面的命令 DESCRIBE INDEXES FOR TABLE tablen
  • 发送多个 HTTP 请求

    我需要编写使用项目 ID 查找有关项目的信息的程序 该 API 一次仅获取一项 因此我只能对每项执行一次查询 API 仅限于五个同时请求 任何额外的结果都会给出 HTTP 429 错误 如果有一个 JavaScript 对象 其中包含所有带