Unexpected Delays in HTTP Requests Using Fetch in Google Cloud Functions

Body:

I’m experiencing unexpected delays when making HTTP requests using fetch in Google Cloud Functions. Despite not awaiting any of the requests, they are not being executed concurrently as expected. Here are some details and a relevant snippet of my code:

Problem Description:

In my Google Cloud Function, I am attempting to make multiple HTTP requests in parallel using fetch. However, I’ve noticed significant delays between these requests, even though I am not awaiting any of them. The delays seem to occur randomly, and the requests are not being sent out simultaneously as intended.

Observed Behavior:

Here’s a sample of the log output showing the delays between the requests:

2024-07-29T15:04:44.371230Z Triggering fetch for document
yQv8lQBVZ9OdyXrPHtB8 – 7/29/2024, 3:04:44 PM
2024-07-29T15:04:43.770616Z Triggering fetch for document
mkjpBSwcSXu4l1p9F7pt – 7/29/2024, 3:04:43 PM
2024-07-29T15:04:43.370684Z Triggering fetch for document
mGYfOXQOtWeztx9DJjAD – 7/29/2024, 3:04:43 PM
2024-07-29T15:04:42.971156Z Triggering fetch for document
kaSfSXvFLQauBdQECTo4 – 7/29/2024, 3:04:42 PM
2024-07-29T15:04:42.770962Z Triggering fetch for document
Z4ZtCxvEQk0UXuRliQgN – 7/29/2024, 3:04:42 PM
2024-07-29T15:04:42.572475Z Triggering fetch for document
V2FZf8NcAwO5pfA7xBMS – 7/29/2024, 3:04:42 PM
2024-07-29T15:04:42.171896Z Triggering fetch for document
OD9vHJeQ5Y1ZsN1wPxP4 – 7/29/2024, 3:04:42 PM
2024-07-29T15:04:41.172708Z Triggering fetch for document
DcCRo2XjRUQ28aPeW4DT – 7/29/2024, 3:04:41 PM
2024-07-29T15:04:41.171919Z Triggering fetch for document
AKi0zSkcDU4lq9F5jerH – 7/29/2024, 3:04:41 PM
2024-07-29T15:04:40.572401Z Triggering fetch for document
1qCtthUVfHZmjEQccSzi – 7/29/2024, 3:04:40 PM
2024-07-29T15:04:40.277409Z Triggering fetch for document
1FaZXjM3rQoySvw3RBFJ – 7/29/2024, 3:04:40 PM

Relevant Code:

Here’s the code snippet where I am making the fetch requests:

const fetch = require('node-fetch');
const functions = require('@google-cloud/functions-framework');
const admin = require('firebase-admin');

admin.initializeApp();

async function triggerFunctionForAllDocuments() {
  const db = admin.firestore();
  const oneMindCollectionRef = db.collection("OneMind");
  const oneMindDocsSnapshot = await oneMindCollectionRef.get();

  const functionUrl =
    "https://us-central1-onemind-95fb2.cloudfunctions.net/processDocument";

  oneMindDocsSnapshot.docs.forEach((doc) => {
    const startTime = new Date();
    console.log(
      `Triggering fetch for document ${doc.id} - ${startTime.toLocaleString()}`
    );

    const agent = functionUrl.startsWith("https") ? httpsAgent : httpAgent;

    fetch(functionUrl, {
      method: "POST",
      body: JSON.stringify({ documentId: doc.id, documentData: doc.data() }),
      headers: { "Content-Type": "application/json" },
    })
      .then((data) => {
        const endTime = new Date();
        console.log(
          `Successfully triggered function for document ${
            doc.id
          } - ${endTime.toLocaleString()} - Duration: ${endTime - startTime}ms`
        );
      })
      .catch((err) => {
        console.error(`Error triggering function for document ${doc.id}:`, err);
      });
  });

  console.log("Triggered Cloud Function for all OneMind documents.");
}

Question:

Why are my fetch requests experiencing delays even though I am not awaiting them and how can I ensure that they are executed concurrently to avoid these delays?

Any insights or suggestions on how to resolve this issue would be greatly appreciated!