| 58 | ) |
| 59 | |
| 60 | func NewWorkerPool(progressCh chan<- any, maxDownloads int) *WorkerPool { |
| 61 | if maxDownloads < 1 { |
| 62 | maxDownloads = 3 // Default to 3 if invalid |
| 63 | } |
| 64 | pool := &WorkerPool{ |
| 65 | taskChan: make(chan string, 100), // We make it buffered to avoid blocking add |
| 66 | progressCh: progressCh, |
| 67 | progressDone: make(chan struct{}), |
| 68 | downloads: make(map[string]*activeDownload), |
| 69 | queued: make(map[string]types.DownloadConfig), |
| 70 | maxDownloads: maxDownloads, |
| 71 | globalLimiter: engine.NewRateLimiter(0, 0), |
| 72 | downloadLimiters: make(map[string]*engine.RateLimiter), |
| 73 | } |
| 74 | for i := 0; i < maxDownloads; i++ { |
| 75 | go pool.worker() |
| 76 | } |
| 77 | return pool |
| 78 | } |
| 79 | |
| 80 | // syncConfigFromState syncs Filename, DestPath, and Mirrors from the associated state. |
| 81 | func syncConfigFromState(cfg *types.DownloadConfig) { |