* 执行限速操作 * @param fn 要执行的操作函数 * @param op 操作类型,用于在遇到 429 时判断是否允许自动重试。默认值 "unknown" 不在白名单内,会被视为不可重试 * @returns 操作结果
(fn: () => Promise<T>, op = "unknown")
| 25 | * @returns 操作结果 |
| 26 | */ |
| 27 | async execute<T>(fn: () => Promise<T>, op = "unknown"): Promise<T> { |
| 28 | // 如果当前运行的操作数已达到上限,则等待 |
| 29 | while (this.running >= this.maxConcurrent) { |
| 30 | await new Promise<void>((resolve) => { |
| 31 | this.queue.push(resolve); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | this.running++; |
| 36 | try { |
| 37 | return await this.executeWithRetry(fn, op); |
| 38 | } finally { |
| 39 | this.running--; |
| 40 | // 执行完成后,从队列中取出下一个等待的操作 |
| 41 | const next = this.queue.shift(); |
| 42 | if (next) { |
| 43 | next(); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * 执行操作并处理 429 错误重试 |
nothing calls this directly
no test coverage detected