Interceptor for rate-limiting requests done via rest.js-compatible clients.
Using this interceptor is pretty straightforward. See below.
var rest = ;var throttler = ;var slowClient = rest; // 1 request per 1000 ms;;;;
In this example, four requests will be performed sequentially, one after another, with 1000 msec delay between.
You can also create multiple unrelated throttlers:
var rest = ;var throttler = ;var slowClient = rest;var verySlowClient = rest;;;;
In this example
verySlowClient have unrelated throttlers, so they are executed in parallel to each other, but still remain sequential within their local queues.
You can share single throttler between several clients:
var rest = ;var throttler = ;var limit = throttler;var muchSlowClient = rest;var verySlackClient = rest;;;;;
These two clients share same limit object, so they belong to single queue and won't be handled in parallel.
By default, interceptor ensures time delta between the last response and next request is no less than specified delay, so if you talk to slow server, you may wait somewhat longer than expected.
If you are not happy with this, you can tell throttler to only account interval between sending requests, disregarding when and if response arrives:
var rest = ;var throttler = ;var slowClient = rest;;
limit property accepts either explicit limit object, or delay number to create limit object automatically. Absent
limit property disables throttling completely.
You can override settings for specific request by assigning configuration hash object to
Following example will uniformly distribute requests between two throttlers, without altering client itself:
var rest = ;var throttler = ;var varyingClient = rest;var slowLimit = throttler;var fastLimit = throttler;for var i = 0; i < 10; i++;
This can be also changed from preceding interceptors, for example, to implement optional rate limiting depending on domain, or whatever.
Using as promise
limit object is actually promise factory. You can use it as follows:
var limit = limit;var queue = ;;;;
limit(delay) returns function returning promise, that will resolve
delay msec after last promise from same throttler had resolved.
It does not work!
You are doing it wrong. Please make sure you either share same
limit object between client instances you want to belong to same rate limiting queue.
rest-throttler.js is made available under the terms of MIT license. See LICENSE for details.