https://github.com/tjenkinson/exponential-rate-limit
A small library which handles decaying exponential backoff. This is useful if you want to start throttling something whilst it is going wrong, but recover once things start working again.
https://github.com/tjenkinson/exponential-rate-limit
backoff decay delay exponential exponential-backoff retry typescript
Last synced: 11 months ago
JSON representation
A small library which handles decaying exponential backoff. This is useful if you want to start throttling something whilst it is going wrong, but recover once things start working again.
- Host: GitHub
- URL: https://github.com/tjenkinson/exponential-rate-limit
- Owner: tjenkinson
- License: apache-2.0
- Created: 2019-03-17T15:27:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-29T19:25:24.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T13:34:03.918Z (over 1 year ago)
- Topics: backoff, decay, delay, exponential, exponential-backoff, retry, typescript
- Language: TypeScript
- Homepage: https://exponential-rate-limit.netlify.com/demo.html
- Size: 549 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/js/exponential-rate-limit)
# Exponential Rate Limit
A small library which handles decaying exponential backoff. This is useful if you want to start throttling something whilst it is going wrong, but recover once things start working again.
## Demo
The demo source is in "[demo.ts](/src/demo.ts)".
To run the demo locally run `npm run start`.
It is also hosted [here](https://exponential-rate-limit.netlify.com/demo.html) (thanks to [Netify](https://www.netlify.com/)).
## Usage
```ts
import { JobQueue, defaultDelayFunction } from './exponential-rate-limit';
const queue = new JobQueue({
/**
* The maximum time (seconds) between jobs executing.
*/
maxInterval: 5
/**
* The delay function.
*/
delayFunction: defaultDelayFunction
});
const { remove } = queue.enqueue(({ enqueueTime } => {
console.log('Executing', enqueueTime);
});
```
## Example
The following example implements `throttledFetch`, which will start delaying future executions exponentially (up to the default 5 seconds) every time a request fails or does not respond with status 200.
Every time there is a 200 response, the delays will also start getting shorter again. As time passes without any jobs being executed, the delay the next job would incur also decreases.
```ts
import { JobQueue } from './exponential-rate-limit';
const jobQueue = new JobQueue(); // default options
function throttledFetch(...fetchArgs): Promise {
return new Promise((resolve) => {
jobQueue.enqueue(() => {
const fetchPromise = fetch(...fetchArgs);
resolve(fetchPromise);
return fetchPromise.then((response) => response.status === 200);
});
});
}
```
## Browser
This can also be used in the browser thanks to [jsDelivr](https://github.com/jsdelivr/jsdelivr):
```html
var queue = new ExponentialRateLimit.JobQueue();
```