RateLimiter in NodeJS

阅读更多
RateLimiter in NodeJS

In the package.json, we need this package

"bottleneck": "^2.18.0"

Here is the import of the module
const Bottleneck = require('bottleneck');
Here is the rate limiter configuration
const limiter = new Bottleneck({
    maxConcurrent: 1,
    minTime: 20
});

1 concurrent client, 1000 milliseconds will send out 1000/20= 50 requests/second

The old await/async code is like this
//await ch.publish(exchange, '', Buffer.from(msg));

The new await/async code with the limiter will be as follow:
await limiter.schedule(() => ch.publish(exchange, '', Buffer.from(msg)) );

References:
https://github.com/jhurliman/node-rate-limiter
https://github.com/SGrondin/bottleneck

你可能感兴趣的:(RateLimiter in NodeJS)