New makes a new *WorkerPool.
(workerCount, jobQueueSize int)
| 30 | |
| 31 | // New makes a new *WorkerPool. |
| 32 | func New(workerCount, jobQueueSize int) *WorkerPool { |
| 33 | if jobQueueSize < 0 { |
| 34 | jobQueueSize = 0 |
| 35 | } |
| 36 | if workerCount < 0 { |
| 37 | workerCount = runtime.NumCPU() |
| 38 | } |
| 39 | pool := WorkerPool{ |
| 40 | pool: make(chan chan func(), workerCount), |
| 41 | jobs: make(chan func(), jobQueueSize), |
| 42 | quit: make(chan struct{}), |
| 43 | wg: sync.WaitGroup{}, |
| 44 | } |
| 45 | for i := 0; i < workerCount; i++ { |
| 46 | var builder workerBuilder |
| 47 | w := builder. |
| 48 | withPool(pool.pool). |
| 49 | withPoolQuit(pool.quit). |
| 50 | withTimeout(0). |
| 51 | withQuit(pool.quit). |
| 52 | build() |
| 53 | w.initWorker(&pool.wg) |
| 54 | } |
| 55 | go pool.dispatch() |
| 56 | return &pool |
| 57 | } |
| 58 | |
| 59 | // Queue queues a job to be run by a worker. |
| 60 | func (pool *WorkerPool) Queue(job func(), timeout time.Duration) bool { |
nothing calls this directly
no test coverage detected