最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - intermittent delays in receiving the image upload streams - Stack Overflow

programmeradmin0浏览0评论

I'm running a Node.js application using KoaJS on AWS Elastic Beanstalk that handles image uploads by streaming the incoming data directly to the Node.js process. Nginx is used as a reverse proxy. In my initial configuration, buffering was disabled so that the stream is passed directly to the Node.js app. The app is managed by PM2 with two instances.

The problem also appears without PM2 and without instance load balancer.

I’ve observed that image uploads sometimes take much longer than expected. Here’s what I see:

  • The first few upload requests seem to gradually increase in duration.
  • Once a “steady state” is reached, uploads typically finish in around 3 seconds.
  • However, occasionally, uploads take 8–9 seconds to complete.

I enabled logging of incoming data chunks on the Node.js side and found that during these delays, the chunks are arriving slowly. I was able to consistently reproduce this behavior using both Node.js and Python clients to send the image data.

I debugged how chunks of data arrive using this code:

app.use(async (ctx, next) => {
    const start = Date.now();
    let lastChunk = start;

    // Intercept the request stream
    console.log("hhh")
    ctx.req.on('data', chunk => {
        const now = Date.now();
        console.log({
            chunkSize: chunk.length,
            timeSinceLastChunk: now - lastChunk,
            totalTime: now - start
        });
        lastChunk = now;
    });

    await next();
});

When running the server locally, the chunks are of 65536 bytes in size. However, when I run the code on ElasticBeanstalk, the chunks sizes are from 1408 to 2816 bytes in size.

What I've tried / additional context:

  • Verified that Nginx buffering is disabled.
  • Also tried enabling Nginx buffering, but the uploads were still slow (Nginx accepted the upload slowly, but my code receives the cached upload quickly).
  • Confirmed that the issue is not due to the application’s internal processing of the image stream.
  • The delay appears to be related to how the incoming HTTP request is handled rather than any post-upload processing.

Question:

What could be causing these intermittent delays in receiving the image upload streams? Has anyone seen similar behavior, and what might be the underlying cause (e.g., related to HTTP protocol features like the 100 Continue handshake, connection handling, or configuration issues with Nginx/Elastic Beanstalk)?

发布评论

评论列表(0)

  1. 暂无评论