poolctl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ a simple task queue for javascript which ensures maximum concurrent tasks are
2
+ less than the pool size it is mostly useful for requesting thousands of
3
+ resources concurrently but avoid and keep maximum connections behind a boundary
4
+ example :
5
+
6
+ ### example
7
+
8
+ ```typescript
9
+ const pool = new Pool(5);
10
+ const urls = [
11
+ "https://www.google.com/1",
12
+ "https://www.google.com/2",
13
+ "https://www.google.com/3",
14
+ "https://www.google.com/4",
15
+ "https://www.google.com/5",
16
+ "https://www.google.com/6",
17
+ "https://www.google.com/7",
18
+ ];
19
+ // here maximum concurrent url being fetched is 5
20
+ await Promise.all(urls.map((u) => pool.exec(() => fetch(u))));
21
+ ```
22
+
23
+ ### Api Docs
24
+
25
+ https://doc.deno.land/https://raw.githubusercontent.com/ehsan2003/task-pool/main/mod.ts
26
+
27
+ the code is simple so if you confused about anything read the code its most of
28
+ the time best way to understand things or just open an issue
@@ -0,0 +1,11 @@
1
+ export declare class FIFOQueue<T> {
2
+ private head;
3
+ private tail;
4
+ private size;
5
+ constructor();
6
+ enqueue(value: T): void;
7
+ dequeue(): T | undefined;
8
+ clear(): void;
9
+ getSize(): number;
10
+ [Symbol.iterator](): Generator<T, void, unknown>;
11
+ }
@@ -0,0 +1,59 @@
1
+ export class FIFOQueue {
2
+ constructor() {
3
+ Object.defineProperty(this, "head", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: null
8
+ });
9
+ Object.defineProperty(this, "tail", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: null
14
+ });
15
+ Object.defineProperty(this, "size", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: 0
20
+ });
21
+ this.clear();
22
+ }
23
+ enqueue(value) {
24
+ const node = { value, next: null };
25
+ if (this.head) {
26
+ this.tail.next = node;
27
+ this.tail = node;
28
+ }
29
+ else {
30
+ this.head = node;
31
+ this.tail = node;
32
+ }
33
+ this.size++;
34
+ }
35
+ dequeue() {
36
+ const current = this.head;
37
+ if (!current) {
38
+ return;
39
+ }
40
+ this.head = this.head.next;
41
+ this.size--;
42
+ return current.value;
43
+ }
44
+ clear() {
45
+ this.head = null;
46
+ this.tail = null;
47
+ this.size = 0;
48
+ }
49
+ getSize() {
50
+ return this.size;
51
+ }
52
+ *[Symbol.iterator]() {
53
+ let current = this.head;
54
+ while (current) {
55
+ yield current.value;
56
+ current = current.next;
57
+ }
58
+ }
59
+ }
package/esm/mod.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ type Task<T> = () => Promise<T>;
2
+ export declare class Pool {
3
+ private size;
4
+ private workingCount;
5
+ private waitQueue;
6
+ constructor(size: number);
7
+ exec<T>(task: Task<T>): Promise<T>;
8
+ private releaseFirstWaiter;
9
+ private waitForAvailability;
10
+ getSize(): number;
11
+ getWorkingCount(): number;
12
+ getPendingCount(): number;
13
+ }
14
+ export {};
package/esm/mod.js ADDED
@@ -0,0 +1,84 @@
1
+ import { FIFOQueue } from "./FIFOQueue.js";
2
+ class Deffered {
3
+ constructor() {
4
+ Object.defineProperty(this, "resolve", {
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true,
8
+ value: void 0
9
+ });
10
+ Object.defineProperty(this, "reject", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "promise", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.promise = new Promise((resolve, reject) => {
23
+ this.reject = reject;
24
+ this.resolve = resolve;
25
+ });
26
+ }
27
+ }
28
+ export class Pool {
29
+ constructor(size) {
30
+ Object.defineProperty(this, "size", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: size
35
+ });
36
+ Object.defineProperty(this, "workingCount", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: 0
41
+ });
42
+ Object.defineProperty(this, "waitQueue", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: new FIFOQueue()
47
+ });
48
+ if (size < 1) {
49
+ throw new Error("Pool size must be greater than 0");
50
+ }
51
+ }
52
+ async exec(task) {
53
+ if (this.workingCount >= this.size) {
54
+ await this.waitForAvailability();
55
+ }
56
+ this.workingCount++;
57
+ try {
58
+ return await task();
59
+ }
60
+ finally {
61
+ this.workingCount--;
62
+ if (this.workingCount < this.size) {
63
+ this.releaseFirstWaiter();
64
+ }
65
+ }
66
+ }
67
+ releaseFirstWaiter() {
68
+ this.waitQueue.dequeue()?.resolve();
69
+ }
70
+ async waitForAvailability() {
71
+ const wait = new Deffered();
72
+ this.waitQueue.enqueue(wait);
73
+ await wait.promise;
74
+ }
75
+ getSize() {
76
+ return this.size;
77
+ }
78
+ getWorkingCount() {
79
+ return this.workingCount;
80
+ }
81
+ getPendingCount() {
82
+ return this.waitQueue.getSize();
83
+ }
84
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "poolctl",
3
+ "version": "0.1.0",
4
+ "description": "A simple concurrency-limiting task pool for JavaScript. Runs at most N async tasks at a time.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/dev-a-loper/task-pool.git"
8
+ },
9
+ "license": "MIT",
10
+ "bugs": {
11
+ "url": "https://github.com/dev-a-loper/task-pool/issues"
12
+ },
13
+ "main": "./script/mod.js",
14
+ "module": "./esm/mod.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./esm/mod.js",
18
+ "require": "./script/mod.js"
19
+ }
20
+ },
21
+ "scripts": {
22
+ "test": "node test_runner.js"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^20.9.0",
26
+ "picocolors": "^1.0.0",
27
+ "@deno/shim-deno": "~0.18.0"
28
+ },
29
+ "_generatedBy": "dnt@0.40.0"
30
+ }
@@ -0,0 +1,11 @@
1
+ export declare class FIFOQueue<T> {
2
+ private head;
3
+ private tail;
4
+ private size;
5
+ constructor();
6
+ enqueue(value: T): void;
7
+ dequeue(): T | undefined;
8
+ clear(): void;
9
+ getSize(): number;
10
+ [Symbol.iterator](): Generator<T, void, unknown>;
11
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FIFOQueue = void 0;
4
+ class FIFOQueue {
5
+ constructor() {
6
+ Object.defineProperty(this, "head", {
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true,
10
+ value: null
11
+ });
12
+ Object.defineProperty(this, "tail", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: null
17
+ });
18
+ Object.defineProperty(this, "size", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: 0
23
+ });
24
+ this.clear();
25
+ }
26
+ enqueue(value) {
27
+ const node = { value, next: null };
28
+ if (this.head) {
29
+ this.tail.next = node;
30
+ this.tail = node;
31
+ }
32
+ else {
33
+ this.head = node;
34
+ this.tail = node;
35
+ }
36
+ this.size++;
37
+ }
38
+ dequeue() {
39
+ const current = this.head;
40
+ if (!current) {
41
+ return;
42
+ }
43
+ this.head = this.head.next;
44
+ this.size--;
45
+ return current.value;
46
+ }
47
+ clear() {
48
+ this.head = null;
49
+ this.tail = null;
50
+ this.size = 0;
51
+ }
52
+ getSize() {
53
+ return this.size;
54
+ }
55
+ *[Symbol.iterator]() {
56
+ let current = this.head;
57
+ while (current) {
58
+ yield current.value;
59
+ current = current.next;
60
+ }
61
+ }
62
+ }
63
+ exports.FIFOQueue = FIFOQueue;
@@ -0,0 +1,14 @@
1
+ type Task<T> = () => Promise<T>;
2
+ export declare class Pool {
3
+ private size;
4
+ private workingCount;
5
+ private waitQueue;
6
+ constructor(size: number);
7
+ exec<T>(task: Task<T>): Promise<T>;
8
+ private releaseFirstWaiter;
9
+ private waitForAvailability;
10
+ getSize(): number;
11
+ getWorkingCount(): number;
12
+ getPendingCount(): number;
13
+ }
14
+ export {};
package/script/mod.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pool = void 0;
4
+ const FIFOQueue_js_1 = require("./FIFOQueue.js");
5
+ class Deffered {
6
+ constructor() {
7
+ Object.defineProperty(this, "resolve", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "reject", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "promise", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.promise = new Promise((resolve, reject) => {
26
+ this.reject = reject;
27
+ this.resolve = resolve;
28
+ });
29
+ }
30
+ }
31
+ class Pool {
32
+ constructor(size) {
33
+ Object.defineProperty(this, "size", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: size
38
+ });
39
+ Object.defineProperty(this, "workingCount", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: 0
44
+ });
45
+ Object.defineProperty(this, "waitQueue", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: new FIFOQueue_js_1.FIFOQueue()
50
+ });
51
+ if (size < 1) {
52
+ throw new Error("Pool size must be greater than 0");
53
+ }
54
+ }
55
+ async exec(task) {
56
+ if (this.workingCount >= this.size) {
57
+ await this.waitForAvailability();
58
+ }
59
+ this.workingCount++;
60
+ try {
61
+ return await task();
62
+ }
63
+ finally {
64
+ this.workingCount--;
65
+ if (this.workingCount < this.size) {
66
+ this.releaseFirstWaiter();
67
+ }
68
+ }
69
+ }
70
+ releaseFirstWaiter() {
71
+ this.waitQueue.dequeue()?.resolve();
72
+ }
73
+ async waitForAvailability() {
74
+ const wait = new Deffered();
75
+ this.waitQueue.enqueue(wait);
76
+ await wait.promise;
77
+ }
78
+ getSize() {
79
+ return this.size;
80
+ }
81
+ getWorkingCount() {
82
+ return this.workingCount;
83
+ }
84
+ getPendingCount() {
85
+ return this.waitQueue.getSize();
86
+ }
87
+ }
88
+ exports.Pool = Pool;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }