NewWorkerPool creates a sharded worker pool. workers: number of workers/shards (0 = NumCPU * 2) queueSize: total queue capacity across all shards (0 = 10000)
(workers, queueSize int, handler func(*net.UDPAddr, []byte))
| 31 | // workers: number of workers/shards (0 = NumCPU * 2) |
| 32 | // queueSize: total queue capacity across all shards (0 = 10000) |
| 33 | func NewWorkerPool(workers, queueSize int, handler func(*net.UDPAddr, []byte)) *WorkerPool { |
| 34 | if workers <= 0 { |
| 35 | workers = runtime.NumCPU() * 2 |
| 36 | } |
| 37 | if queueSize <= 0 { |
| 38 | queueSize = 10000 |
| 39 | } |
| 40 | |
| 41 | queuePerShard := queueSize / workers |
| 42 | if queuePerShard < 100 { |
| 43 | queuePerShard = 100 |
| 44 | } |
| 45 | |
| 46 | p := &WorkerPool{ |
| 47 | queues: make([]chan WorkItem, workers), |
| 48 | handler: handler, |
| 49 | workers: workers, |
| 50 | queuePerShard: queuePerShard, |
| 51 | dropped: make([]uint64, workers), |
| 52 | } |
| 53 | |
| 54 | for i := 0; i < workers; i++ { |
| 55 | p.queues[i] = make(chan WorkItem, queuePerShard) |
| 56 | } |
| 57 | |
| 58 | return p |
| 59 | } |
| 60 | |
| 61 | // Start launches all worker goroutines. |
| 62 | func (p *WorkerPool) Start() { |
no outgoing calls