Files
read_book/src/main/utils/concurrencyManager.ts
寒寒 455dd1f4cd feat(desktop): 实现一些功能
1. 实现任务暂停功能

2. 实现页面的国际化功能

3.优化项目的结构以及BUG

4. 优化系统架构

5. 实现一大堆的功能
2026-01-25 03:30:23 +08:00

61 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pLimit from 'p-limit' // 建议使用 v2.2.0 以兼容 CJS
/**
* 并发管理器,负责动态调整并发数
*/
export class ConcurrencyManager {
private baseConcurrency: number
private currentConcurrency: number
private limit: ReturnType<typeof pLimit>
constructor(baseConcurrency: number = 2) {
this.baseConcurrency = baseConcurrency
this.currentConcurrency = baseConcurrency
this.limit = pLimit(this.currentConcurrency)
}
/**
* 获取当前的并发限制器
*/
getLimit() {
return this.limit
}
/**
* 增加并发数
*/
increaseConcurrency(): void {
this.currentConcurrency++
this.limit = pLimit(this.currentConcurrency)
}
/**
* 减少并发数
*/
decreaseConcurrency(): void {
if (this.currentConcurrency > 1) {
this.currentConcurrency--
this.limit = pLimit(this.currentConcurrency)
}
}
/**
* 重置并发数为基准值
*/
resetConcurrency(): void {
this.currentConcurrency = this.baseConcurrency
this.limit = pLimit(this.currentConcurrency)
}
/**
* 根据系统资源动态调整并发数
*/
adjustConcurrency(): void {
// 这里可以添加根据系统资源如CPU、内存使用率动态调整并发数的逻辑
// 目前实现一个简单的基于任务类型的调整策略
this.resetConcurrency()
}
}
// 导出一个单例实例
export const concurrencyManager = new ConcurrencyManager()