Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of requests at a time with RxJS

Suppose I have 10 urls, and I wish to make an HTTP request for each one of them.

I could create an observable of the URLs, then .flatMap() the requests for each one of them, and then .subscribe for the results. But that would make all of the requests at once.

Is there a way to impose a limit to the number of requests to a fixed number, in order not to overload the server

like image 771
thalesmello Avatar asked Dec 30 '15 19:12

thalesmello


2 Answers

RxJS v6 update

pipe through mergeMap with your parallel limit as 2nd parameter

const MAX_PARALLEL_QUERIES = 3;
let allResults = [];
let observables = [] // fill with observables
from(observables)
            .pipe(mergeMap(observable => observable, MAX_PARALLEL_QUERIES))
            .subscribe(
                partialResults => {
                    allResults = allResults.concat(partialResults);
                },
                err => {
                    // handle error
                },
                () => {
                    // get here when all obserable has returned
                    allResults.forEach(result=> {
                        // do what you want
                    });
                }
            );
like image 175
Eylon Sultan Avatar answered Sep 25 '22 06:09

Eylon Sultan


It's 2018, rxjs 5 is here and this is how I solved it

urls$
  .mergeMap((url) => request({ url }), null, 10)
  .subscribe()

mergeMap (aka flatMap) already takes the "max concurrency" as its 3rd parameter (see the docs)

Btw. I am using universal-rxjs-ajax (the request) for node compatibility, but it should work the same with Observable.ajax

like image 34
sra Avatar answered Sep 25 '22 06:09

sra