com.jimuwd.xian.registry-proxy 1.1.14 → 1.1.16

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.
@@ -1,8 +1,10 @@
1
- export default class ConcurrencyLimiter {
1
+ export default class ReentrantConcurrencyLimiter {
2
2
  readonly maxConcurrency: number;
3
3
  private current;
4
4
  private queue;
5
+ private executionContext;
5
6
  constructor(maxConcurrency: number);
7
+ private getContextId;
6
8
  acquire(): Promise<void>;
7
9
  release(): void;
8
10
  run<T>(task: () => Promise<T>): Promise<T>;
@@ -1,34 +1,52 @@
1
- export default class ConcurrencyLimiter {
1
+ export default class ReentrantConcurrencyLimiter {
2
2
  maxConcurrency;
3
3
  current = 0;
4
4
  queue = [];
5
+ executionContext = new WeakMap();
5
6
  constructor(maxConcurrency) {
6
- if (maxConcurrency <= 0) {
7
+ if (maxConcurrency <= 0)
7
8
  throw new Error("maxConcurrency must be positive");
8
- }
9
9
  this.maxConcurrency = maxConcurrency;
10
10
  }
11
+ getContextId() {
12
+ return {}; // 每次调用生成唯一对象引用
13
+ }
11
14
  async acquire() {
15
+ const contextId = this.getContextId();
16
+ const depth = this.executionContext.get(contextId) || 0;
17
+ if (depth > 0) {
18
+ this.executionContext.set(contextId, depth + 1);
19
+ return;
20
+ }
12
21
  if (this.current < this.maxConcurrency) {
13
22
  this.current++;
23
+ this.executionContext.set(contextId, 1);
14
24
  return;
15
25
  }
16
26
  return new Promise((resolve) => {
17
- this.queue.push(resolve);
27
+ this.queue.push(() => {
28
+ this.current++;
29
+ this.executionContext.set(contextId, 1);
30
+ resolve();
31
+ });
18
32
  });
19
33
  }
20
34
  release() {
21
- if (this.current <= 0) {
35
+ const contextId = this.getContextId();
36
+ const depth = this.executionContext.get(contextId);
37
+ if (depth === undefined) {
22
38
  throw new Error("release() called without acquire()");
23
39
  }
40
+ if (depth > 1) {
41
+ this.executionContext.set(contextId, depth - 1);
42
+ return;
43
+ }
44
+ this.executionContext.delete(contextId);
24
45
  this.current--;
25
- const next = this.queue.shift();
26
- if (next) {
27
- // 异步执行,避免递归调用栈溢出
28
- Promise.resolve().then(() => {
29
- this.current++;
30
- next();
31
- });
46
+ if (this.queue.length > 0) {
47
+ const next = this.queue.shift();
48
+ // 仍异步执行,但需确保顺序
49
+ Promise.resolve().then(next);
32
50
  }
33
51
  }
34
52
  async run(task) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.1.14",
4
- "description": "A lightweight npm registry proxy with fallback support",
3
+ "version": "1.1.16",
4
+ "description": "A lightweight npm registry local proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/server/index.js",
7
7
  "bin": {