package utils

import (
	"sync"
)

// DefaultMaxWorkers 默认并发数
const DefaultMaxWorkers = 32

// WorkerPool 并发执行一批任务。
func WorkerPool(tasks []interface{}, taskFunc func(interface{}), maxWorkers ...int) {
	if len(tasks) == 0 {
		return
	}

	workers := DefaultMaxWorkers
	if len(maxWorkers) > 0 && maxWorkers[0] > 0 {
		workers = maxWorkers[0]
	}
	if workers > len(tasks) {
		workers = len(tasks)
	}

	jobs := make(chan interface{}, len(tasks))
	var wg sync.WaitGroup
	for i := 0; i < workers; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for task := range jobs {
				taskFunc(task)
			}
		}()
	}
	for _, task := range tasks {
		jobs <- task
	}

	close(jobs)
	wg.Wait()
}
