@mangosteen/rate-limiter
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -30,2 +30,7 @@ import { TimeoutScheduler, VoidCallback } from './models'; | ||
}; | ||
/** | ||
* Restore the specified number of tokens. | ||
* @param count The number of tokens to restore. | ||
*/ | ||
restoreTokens(count: number): void; | ||
private _recreateWatcher; | ||
@@ -32,0 +37,0 @@ private _rejectAllAwaiters; |
@@ -41,2 +41,9 @@ "use strict"; | ||
} | ||
/** | ||
* Restore the specified number of tokens. | ||
* @param count The number of tokens to restore. | ||
*/ | ||
restoreTokens(count) { | ||
this._tokenBucket.restoreTokens(count); | ||
} | ||
_recreateWatcher() { | ||
@@ -43,0 +50,0 @@ // Get previous watcher |
@@ -64,2 +64,7 @@ import redBlackTree from 'functional-red-black-tree'; | ||
}; | ||
/** | ||
* Restore the specified number of tokens. | ||
* @param count The number of tokens to restore. | ||
*/ | ||
restoreTokens(count: number): void; | ||
protected _onTokensConsumed(_count: number): void; | ||
@@ -66,0 +71,0 @@ /** Restore tokens using continuous token restorers. */ |
@@ -52,4 +52,3 @@ "use strict"; | ||
this._discreteTokenRestorers.set(restorer, restorer.subscribe(count => { | ||
this._addTokensToBucket(count); | ||
this._checkListeners(); | ||
this.restoreTokens(count); | ||
})); | ||
@@ -181,2 +180,10 @@ return true; | ||
} | ||
/** | ||
* Restore the specified number of tokens. | ||
* @param count The number of tokens to restore. | ||
*/ | ||
restoreTokens(count) { | ||
this._addTokensToBucket(count); | ||
this._checkListeners(); | ||
} | ||
_onTokensConsumed(_count) { | ||
@@ -183,0 +190,0 @@ // Need to reschedule timers because available tokens changed. |
{ | ||
"name": "@mangosteen/rate-limiter", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A rate limiter that helps you limit your client from making excessive API requests.", | ||
@@ -11,7 +11,8 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "tsc", | ||
"start": "ts-node ./src/test.ts", | ||
"test-publish-npm": "npm pack", | ||
"publish-npm": "npm publish --access public", | ||
"prepare": "tsc" | ||
"prepublish": "npm run build", | ||
"start": "ts-node ./src/test.ts" | ||
}, | ||
@@ -52,7 +53,8 @@ "repository": { | ||
"nanotimer": "^0.3.15", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^10.2.1", | ||
"typescript": "^4.4.2" | ||
"typescript": "^4.4.3" | ||
}, | ||
"dependencies": { | ||
"denque": "^2.0.0", | ||
"denque": "^2.0.1", | ||
"functional-red-black-tree": "^1.0.1", | ||
@@ -59,0 +61,0 @@ "just-performance": "^4.3.0", |
@@ -22,6 +22,9 @@ # rate-limiter | ||
Create a token bucket. It holds tokens that represent a numeric cost for launching jobs. For example, you can create a bucket with 10 tokens. You can consume 1 token to launch a job with a cost of 1. Other jobs might consume more/less tokens, whatever makes more sense for your application. | ||
Create a token bucket. It holds tokens that represent a numeric cost of launching jobs. For example, you can create a bucket with 10 tokens. You can consume 1 token to launch a job with a cost of 1. Other jobs might consume more/less tokens, whatever makes more sense for your application. | ||
```ts | ||
import { RollingWindowTokenBucket, TokenBucket } from '@mangosteen/rate-limiter'; | ||
import { | ||
RollingWindowTokenBucket, | ||
TokenBucket | ||
} from '@mangosteen/rate-limiter'; | ||
@@ -38,3 +41,3 @@ // This token bucket does not care about history, | ||
// last `X` milliseconds, and you may limit how many jobs | ||
// could be launched during the history window. | ||
// can be launched during the history window. | ||
const windowTokenBucket = new RollingWindowTokenBucket({ | ||
@@ -53,3 +56,6 @@ initialTokens: 10, | ||
```ts | ||
import { ContinuousTokenRestorer, PeriodicTokenRestorer } from '@mangosteen/rate-limiter'; | ||
import { | ||
ContinuousTokenRestorer, | ||
PeriodicTokenRestorer | ||
} from '@mangosteen/rate-limiter'; | ||
@@ -74,5 +80,5 @@ // Periodic token restorers restore tokens ... periodically! | ||
// Continuous token restorers restore tokens ... continuously! | ||
// At any instant, we can compute how many (partial) tokens should | ||
// be restored. Our rate limiter can figure out the proper scheduling | ||
// to make this work. | ||
// At any instant, we can compute how many (partial) tokens | ||
// should be restored. Our token buckets can figure out the | ||
// proper scheduling to make this work. | ||
const continuousRestorer = new ContinuousTokenRestorer({ | ||
@@ -86,4 +92,4 @@ rate: { | ||
// And last but not least, you can add arbitrary number of restorers | ||
// to a single bucket! | ||
// And last but not least, you can add arbitrary number of | ||
// restorers to a single bucket! | ||
bucket.addTokenRestorer(periodicRestorer1); | ||
@@ -169,3 +175,9 @@ bucket.addTokenRestorer(periodicRestorer2); | ||
```ts | ||
import { IntervalScheduler, TimeoutScheduler, TokenBucket, RollingWindowTokenBucket, PeriodicTokenRestorer } from '@mangosteen/rate-limiter'; | ||
import { | ||
IntervalScheduler, | ||
TimeoutScheduler, | ||
TokenBucket, | ||
RollingWindowTokenBucket, | ||
PeriodicTokenRestorer | ||
} from '@mangosteen/rate-limiter'; | ||
import NanoTimer from 'nanotimer'; | ||
@@ -172,0 +184,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
72381
881
226
8
Updateddenque@^2.0.1