MCPcopy Create free account
hub / github.com/dc0d/workerpool / New

Function New

workerpool.go:32–57  ·  view source on GitHub ↗

New makes a new *WorkerPool.

(workerCount, jobQueueSize int)

Source from the content-addressed store, hash-verified

30
31// New makes a new *WorkerPool.
32func 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.
60func (pool *WorkerPool) Queue(job func(), timeout time.Duration) bool {

Callers

nothing calls this directly

Calls 7

withPoolMethod · 0.95
dispatchMethod · 0.95
buildMethod · 0.80
withQuitMethod · 0.80
withTimeoutMethod · 0.80
withPoolQuitMethod · 0.80
initWorkerMethod · 0.80

Tested by

no test coverage detected