TL;DR - I'm Looking for a way to control the number of HTTP requests concurrent connections to a REST API while I use RxJS.
My Node.js app will make a few thousand REST API calls to a third party provider. However, I know that if I make all those requests at once, the service might go down or reject my requests because of DDoS attack. So, I want to set the max number of concurrent connection at any given time. I used to implement concurrency control with Promises by leveraging Throat Package, but I haven't found a similar way to implement this.
I tried to use merge
with 1 for concurrence as suggested in this post How to limit the concurrency of flatMap?, but all requests are sent at once.
Here's my code:
var Rx = require('rx'),
rp = require('request-promise');
var array = ['https://httpbin.org/ip', 'https://httpbin.org/user-agent',
'https://httpbin.org/delay/3',
'https://httpbin.org/delay/3',
'https://httpbin.org/delay/3'
];
var source = Rx.Observable.fromArray(array).map(httpGet).merge(1);
function httpGet(url) {
return rp.get(url);
}
var results = [];
var subscription = source.subscribe(
function (x) {
console.log('=====', x, '======');
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});