https://github.com/langri-sha/ajax-limited
A rate-limited version of $.ajax with support for slow start on wakeup
https://github.com/langri-sha/ajax-limited
Last synced: 3 months ago
JSON representation
A rate-limited version of $.ajax with support for slow start on wakeup
- Host: GitHub
- URL: https://github.com/langri-sha/ajax-limited
- Owner: langri-sha
- License: bsd-3-clause
- Created: 2017-10-31T13:39:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-26T17:04:58.000Z (over 7 years ago)
- Last Synced: 2025-01-08T09:16:20.017Z (5 months ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ajax-limited
============
[](https://travis-ci.org/toggl/ajax-limited)
[](https://coveralls.io/github/toggl/ajax-limited?branch=master)
- - -
A rate-limited version of $.ajax with support for slow start on wakeup.## Installing
```
npm install --save ajax-limited
```## Usage
```javascript
var $ = require('jquery');
var ajaxLimited = require('ajax-limited');ajaxLimited.configure($, {
bucketSize: 9,
tokensPerInterval: 9,
interval: 3000,
});ajaxLimited.get({
bucketSize: 2,
tokensPerInterval: 2,
interval: 3000,
});
```## Documentation
### AjaxLimitedManages the set of `TokenBucket`s limitting AJAX requests.
#### Params:
* **Object** *[options]* Optional root `TokenBucket` options
### .prototype.configure(target, [options])
Configures rate limitting into an object.
#### Params:
* **Object** *target*
* **Object** *[options]*### .prototype.restore([target])
Restores the original ajax method on the configured targets or a target
object.#### Params:
* **Object** *[target]*
#### Return:
* **Function** originalAjax
### .prototype.slowStart(options)
Receives initial options for each HTTP method (or all of them), modifies
buckets so they start with them and slowly transitions rate limitting until
the original rates are reached.#### Params:
* **Object** *options*
#### Example
```javascript
ajaxLimited.slowStart({
get: { // Slows down get to this rate
bucketSize: 9,
tokensPerInterval: 9
},
'put,patch,post,delete': { // Slows down post, put, patch, delete to this rate
bucketSize: 9,
tokensPerInterval: 0,
},
transitionTime: 9000, // Takes 9s to reach normal speed
interval: 3000 // Updates speed every 3s
});
```