com.jimuwd.xian.registry-proxy 1.1.16 → 1.1.17
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.
|
@@ -2,51 +2,50 @@ export default class ReentrantConcurrencyLimiter {
|
|
|
2
2
|
maxConcurrency;
|
|
3
3
|
current = 0;
|
|
4
4
|
queue = [];
|
|
5
|
-
|
|
5
|
+
executionStack = [];
|
|
6
6
|
constructor(maxConcurrency) {
|
|
7
|
-
if (maxConcurrency <= 0)
|
|
7
|
+
if (maxConcurrency <= 0) {
|
|
8
8
|
throw new Error("maxConcurrency must be positive");
|
|
9
|
+
}
|
|
9
10
|
this.maxConcurrency = maxConcurrency;
|
|
10
11
|
}
|
|
11
12
|
getContextId() {
|
|
12
|
-
return
|
|
13
|
+
return Symbol('context');
|
|
13
14
|
}
|
|
14
15
|
async acquire() {
|
|
15
16
|
const contextId = this.getContextId();
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
18
|
-
|
|
17
|
+
const existingContext = this.executionStack.find(c => c.contextId === contextId);
|
|
18
|
+
if (existingContext) {
|
|
19
|
+
existingContext.depth++;
|
|
19
20
|
return;
|
|
20
21
|
}
|
|
21
22
|
if (this.current < this.maxConcurrency) {
|
|
22
23
|
this.current++;
|
|
23
|
-
this.
|
|
24
|
+
this.executionStack.push({ contextId, depth: 1 });
|
|
24
25
|
return;
|
|
25
26
|
}
|
|
26
27
|
return new Promise((resolve) => {
|
|
27
28
|
this.queue.push(() => {
|
|
28
29
|
this.current++;
|
|
29
|
-
this.
|
|
30
|
+
this.executionStack.push({ contextId, depth: 1 });
|
|
30
31
|
resolve();
|
|
31
32
|
});
|
|
32
33
|
});
|
|
33
34
|
}
|
|
34
35
|
release() {
|
|
35
|
-
|
|
36
|
-
const depth = this.executionContext.get(contextId);
|
|
37
|
-
if (depth === undefined) {
|
|
36
|
+
if (this.executionStack.length === 0) {
|
|
38
37
|
throw new Error("release() called without acquire()");
|
|
39
38
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
const lastContext = this.executionStack[this.executionStack.length - 1];
|
|
40
|
+
lastContext.depth--;
|
|
41
|
+
if (lastContext.depth === 0) {
|
|
42
|
+
this.executionStack.pop();
|
|
43
|
+
this.current--;
|
|
44
|
+
if (this.queue.length > 0) {
|
|
45
|
+
const next = this.queue.shift();
|
|
46
|
+
if (next)
|
|
47
|
+
next();
|
|
48
|
+
}
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
async run(task) {
|