@antti5/ship-tool-common 1.0.3 → 1.0.4
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/lib/Semaphore.js +34 -10
- package/package.json +1 -1
package/lib/Semaphore.js
CHANGED
|
@@ -5,7 +5,7 @@ class Semaphore {
|
|
|
5
5
|
#queue = [];
|
|
6
6
|
|
|
7
7
|
#rateLimit;
|
|
8
|
-
#
|
|
8
|
+
#rateLimitInterval;
|
|
9
9
|
|
|
10
10
|
#waiters = [];
|
|
11
11
|
|
|
@@ -15,14 +15,23 @@ class Semaphore {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
acquire({ jobClass = null } = {}) {
|
|
18
|
-
|
|
18
|
+
|
|
19
|
+
/* If the semaphore is not at maximum value and no rate limit interval
|
|
20
|
+
has been set, return immediately.
|
|
21
|
+
|
|
22
|
+
If the semaphore is rate-limited, then set interval so that the next
|
|
23
|
+
acquisition will be timed. */
|
|
24
|
+
|
|
25
|
+
if (this.#value < this.#max && !this.#rateLimitInterval) {
|
|
26
|
+
if (this.#rateLimit)
|
|
27
|
+
this.#rateLimitInterval = setInterval(
|
|
28
|
+
() => this.#resolveNext(),
|
|
29
|
+
1000 / this.#rateLimit
|
|
30
|
+
);
|
|
19
31
|
this.#value++;
|
|
20
|
-
return
|
|
32
|
+
return;
|
|
21
33
|
}
|
|
22
34
|
|
|
23
|
-
if (this.#rateLimit && !this.#resolveInterval)
|
|
24
|
-
this.#resolveInterval = setInterval(() => this.#resolveNext(), 1000 / this.#rateLimit);
|
|
25
|
-
|
|
26
35
|
return new Promise((resolve, reject) =>
|
|
27
36
|
this.#queue.push({ jobClass, resolve, reject })
|
|
28
37
|
);
|
|
@@ -45,9 +54,9 @@ class Semaphore {
|
|
|
45
54
|
this.#queue.shift().resolve();
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
if (this.#queue.length === 0 && this.#
|
|
49
|
-
clearInterval(this.#
|
|
50
|
-
this.#
|
|
57
|
+
if (this.#queue.length === 0 && this.#rateLimitInterval) {
|
|
58
|
+
clearInterval(this.#rateLimitInterval);
|
|
59
|
+
this.#rateLimitInterval = null;
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
62
|
|
|
@@ -65,7 +74,14 @@ class Semaphore {
|
|
|
65
74
|
raiseJobClass(jobClass) {
|
|
66
75
|
this.#queue = [
|
|
67
76
|
...this.#queue.filter(job => job.jobClass === jobClass),
|
|
68
|
-
...this.#queue.filter(job =>
|
|
77
|
+
...this.#queue.filter(job => job?.jobClass !== jobClass)
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
lowerJobClass(jobClass) {
|
|
82
|
+
this.#queue = [
|
|
83
|
+
...this.#queue.filter(job => job?.jobClass !== jobClass),
|
|
84
|
+
...this.#queue.filter(job => job.jobClass === jobClass)
|
|
69
85
|
];
|
|
70
86
|
}
|
|
71
87
|
|
|
@@ -81,6 +97,14 @@ class Semaphore {
|
|
|
81
97
|
this.#max = max;
|
|
82
98
|
}
|
|
83
99
|
|
|
100
|
+
getRateLimit() {
|
|
101
|
+
return this.#rateLimit;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
setRateLimit(rateLimit) {
|
|
105
|
+
this.#rateLimit = rateLimit;
|
|
106
|
+
}
|
|
107
|
+
|
|
84
108
|
async run(func, { jobClass = null } = {}) {
|
|
85
109
|
try {
|
|
86
110
|
await this.acquire({ jobClass });
|