4 Commits

Author SHA1 Message Date
Michael Fabian 'Xaymar' Dirks 3483a6c60e Add important comments to document functionality 2023-03-19 00:41:06 +01:00
Michael Fabian 'Xaymar' Dirks ea5d7239dd Publish v0.2.0
- Make RateLimiter the default export
2023-03-19 00:27:48 +01:00
Michael Fabian 'Xaymar' Dirks 1cf89ebcf6 Make RateLimiter the default export 2023-03-19 00:24:07 +01:00
Michael Fabian 'Xaymar' Dirks 54a14456e1 Add README 2023-03-19 00:21:20 +01:00
3 changed files with 91 additions and 7 deletions
+66
View File
@@ -0,0 +1,66 @@
# RateLimiter
Simple but effective way to rate limit Tasks in JavaScript. Anything can be rate limited,
## Features
- Rate limiting for anything!
- Looks nice I guess?
- That's about it.
## Usage
```js
var RateLimiter = require("@xaymar/ratelimiter");
let limitMany = new RateLimiter(4);
let limitOne = new RateLimiter(1);
for (let idx = 0; idx < 3; idx++) {
limitOne.queue(async () => {
console.log("Only 1 of this can occur every 1s.");
await new Promise((resolve, reject) => {setTimeout(() => {resolve();}, 1000);});
});
}
for (let idx = 0; idx < 10; idx++) {
limitMany.queue(async () => {
console.log("This however can occur many times.");
await new Promise((resolve, reject) => {setTimeout(() => {resolve();}, 1000);});
});
}
```
## FAQ
### Why did you create this?
Multiple reasons, but here's two of the biggest examples:
1. A script that was supposed to automatically help me generate the proper Copyright notice headers ended up deleting files, or creating empty headers. Limiting the numbers of sub-processes to 1 for the version control binary, and the number of parallel file handles to the number of CPUs significantly improved the stability. No more empty files, no more empty headers!
2. Some resources are only available in limited quantity, such as encoder instances on NVIDIA GPUs, or CPU cores. Often it makes sense to rate limit to that limit, instead of pushing as much data through as possible and then ending up slower than if you did everything sequentially. Especially when it comes to heavy and complex tasks, like encoding.
### Does this support WebWorkers?
No, but it is relatively easy to do without official support. See the example below:
```js
// main.js
var RateLimiter = require("@xaymar/ratelimiter");
let worker = new Worker("worker.js");
let workerRL = new RateLimiter(1);
worker.onmessage = (event) => {
worker.resolve(event);
}
workerRL.queue(async () => {
return await new Promise((resolve, reject) => {
worker.resolve = resolve;
worker.reject = reject;
worker.postMessage("Request");
})
});
// worker.js
self.onmessage = (event) => {
self.postMessage("Reply");
}
```
## License
Available under GPLv3 as well as a commercial license. Contact `info@xaymar.com` for more information.
+8 -3
View File
@@ -1,7 +1,14 @@
{ {
"name": "@xaymar/ratelimiter", "name": "@xaymar/ratelimiter",
"version": "0.1.0", "author": "Michael Fabian 'Xaymar' Dirks <info@xaymar.com>",
"description": "A simple but effective way to rate limit Tasks in JavaScript.", "description": "A simple but effective way to rate limit Tasks in JavaScript.",
"license": "GPLv3",
"repository": "https://github.com/Xaymar/js-ratelimiter",
"version": "0.2.1",
"funding": {
"type": "patreon",
"url": "https://www.patreon.com/xaymar"
},
"main": "generated/ratelimiter.js", "main": "generated/ratelimiter.js",
"scripts": { "scripts": {
"lint": "npx eslint ./source --ext .ts,.mts", "lint": "npx eslint ./source --ext .ts,.mts",
@@ -11,8 +18,6 @@
"prepublish": "npm run build", "prepublish": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "Michael Fabian 'Xaymar' Dirks <info@xaymar.com>",
"license": "GPLv3",
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/node": "^18.15.3", "@types/node": "^18.15.3",
+17 -4
View File
@@ -24,15 +24,22 @@ interface RateLimiterInstance {
solver?: Promise<any>, solver?: Promise<any>,
} }
export type RateLimiterAsyncExecutor = (...args: any[]) => Promise<any>; type RateLimiterAsyncExecutor = (...args: any[]) => Promise<any>;
export type RateLimiterSyncExecutor = (...args: any[]) => any; type RateLimiterSyncExecutor = (...args: any[]) => any;
export type RateLimiterExecutor = RateLimiterSyncExecutor | RateLimiterAsyncExecutor; type RateLimiterExecutor = RateLimiterSyncExecutor | RateLimiterAsyncExecutor;
export class RateLimiter { /** A simple but effective way to rate limit Tasks.
*
*/
export default class RateLimiter {
private _maximum: number = 0; private _maximum: number = 0;
private _available: number = 0; private _available: number = 0;
private _instances: any[]; private _instances: any[];
/** Create a new instance of a RateLimiter.
*
* @param limit The maximum number of tasks that should run in parallel. The Default is 2/3rds of the available CPU threads.
*/
constructor(limit?: number) { constructor(limit?: number) {
if (!limit) { if (!limit) {
this._maximum = Math.ceil(Math.max(1, os.cpus().length / 3 * 2)); this._maximum = Math.ceil(Math.max(1, os.cpus().length / 3 * 2));
@@ -43,6 +50,12 @@ export class RateLimiter {
this._instances = []; this._instances = [];
} }
/** Queue up a new task.
*
* @param executor The function that should be run eventually.
* @param args Optional arguments to pass to the function.
* @returns A Promise that resolves with the result of the function.
*/
async queue(executor: RateLimiterExecutor, ...args: any[]) { async queue(executor: RateLimiterExecutor, ...args: any[]) {
// Use async/await to find a free slot. // Use async/await to find a free slot.
while (this._available == 0) { while (this._available == 0) {