我如何限制node.js中服务器的上传速度?

时间:2022-04-29 17:13:02

How would I limit upload speed from the server in node.js?

我如何限制node.js中服务器的上传速度?

Is this even an option?

这甚至是一个选择吗?

Scenario: I'm writing some methods to allow users to automated-ly upload files to my server. I want to limit the upload speed to (for instance) 50kB/s (configurable of course).

场景:我正在编写一些方法,允许用户自动将文件上传到我的服务器。我想将上传速度限制为(例如)50kB / s(当然可配置)。

3 个解决方案

#1


15  

I do not think you can force a client to stream at a predefined speed, however you can control the "average speed" of the entire process.

我认为您不能强制客户端以预定义的速度进行流式传输,但您可以控制整个过程的“平均速度”。

var startTime  = Date.now(),
    totalBytes = ..., //NOTE: you need the client to give you the total amount of incoming bytes
    curBytes   = 0;

stream.on('data', function(chunk) { //NOTE: chunk is expected to be a buffer, if string look for different ways to get bytes written
     curBytes += chunk.length;
     var offsetTime = calcReqDelay(targetUploadSpeed);
     if (offsetTime > 0) {
         stream.pause();
         setTimeout(offsetTime, stream.resume);
     }
});

function calcReqDelay(targetUploadSpeed) { //speed in bytes per second
    var timePassed = Date.now() - startTime;
    var targetBytes = targetUploadSpeed * timePassed / 1000;
    //calculate how long to wait (return minus in case we actually should be faster)
    return waitTime;
}

This is of course pseudo code, but you probably get the point. There may be another, and better, way which I do not know about. In such case, I hope someone else will point it out.

这当然是伪代码,但你可能得到了重点。可能还有另一种更好的方式,我不知道。在这种情况下,我希望其他人会指出它。

Note that it is also not very precise, and you may want to have a different metric than the average speed.

请注意,它也不是很精确,您可能希望使用与平均速度不同的度量标准。

#2


6  

Use throttle module to control the pipe stream speed

使用节流模块来控制管道流速度

npm install throttle

npm安装油门

var Throttle = require('throttle');

// create a "Throttle" instance that reads at 1 b/s 
var throttle = new Throttle(1);

req.pipe(throttle).pipe(gzip).pipe(res);

#3


2  

Instead of rolling your own, the normal way to do this in production, is to let your load balancer or entry server throttle the incoming requests. See http://en.wikipedia.org/wiki/Bandwidth_throttling. It's not typically something an application needs to handle itself.

在生产中执行此操作的常规方法是让负载均衡器或入口服务器限制传入请求,而不是自己滚动。请参见http://en.wikipedia.org/wiki/Bandwidth_throttling。它通常不是应用程序需要处理的东西。

#1


15  

I do not think you can force a client to stream at a predefined speed, however you can control the "average speed" of the entire process.

我认为您不能强制客户端以预定义的速度进行流式传输,但您可以控制整个过程的“平均速度”。

var startTime  = Date.now(),
    totalBytes = ..., //NOTE: you need the client to give you the total amount of incoming bytes
    curBytes   = 0;

stream.on('data', function(chunk) { //NOTE: chunk is expected to be a buffer, if string look for different ways to get bytes written
     curBytes += chunk.length;
     var offsetTime = calcReqDelay(targetUploadSpeed);
     if (offsetTime > 0) {
         stream.pause();
         setTimeout(offsetTime, stream.resume);
     }
});

function calcReqDelay(targetUploadSpeed) { //speed in bytes per second
    var timePassed = Date.now() - startTime;
    var targetBytes = targetUploadSpeed * timePassed / 1000;
    //calculate how long to wait (return minus in case we actually should be faster)
    return waitTime;
}

This is of course pseudo code, but you probably get the point. There may be another, and better, way which I do not know about. In such case, I hope someone else will point it out.

这当然是伪代码,但你可能得到了重点。可能还有另一种更好的方式,我不知道。在这种情况下,我希望其他人会指出它。

Note that it is also not very precise, and you may want to have a different metric than the average speed.

请注意,它也不是很精确,您可能希望使用与平均速度不同的度量标准。

#2


6  

Use throttle module to control the pipe stream speed

使用节流模块来控制管道流速度

npm install throttle

npm安装油门

var Throttle = require('throttle');

// create a "Throttle" instance that reads at 1 b/s 
var throttle = new Throttle(1);

req.pipe(throttle).pipe(gzip).pipe(res);

#3


2  

Instead of rolling your own, the normal way to do this in production, is to let your load balancer or entry server throttle the incoming requests. See http://en.wikipedia.org/wiki/Bandwidth_throttling. It's not typically something an application needs to handle itself.

在生产中执行此操作的常规方法是让负载均衡器或入口服务器限制传入请求,而不是自己滚动。请参见http://en.wikipedia.org/wiki/Bandwidth_throttling。它通常不是应用程序需要处理的东西。