github.com/dubbogo/gost@v1.14.0/sync/README.md (about) 1 # Sync 2 3 ## WorkerPool 4 5 WorkerPool is an interface which defined the behaviors of worker pool. 6 7 ## baseWorkerPool 8 9 baseWorkerPool is a versatile goroutine pool with multiple queues. You may refer to [base_worker_pool.go](./base_worker_pool.go) for the architecture. **`baseWorkerPool`** provides some basic functions, like initialization, shutdown, etc. Developers are allowed to determine how to dispatch tasks to each queue, for example, dispatch the tasks to each queue fairly using Round Robin. 10 11 ### Worker LifeCycle 12 13 **`p.dispatch(numWorkers, wg)`** creates workers and dispatches them to each queue equally. The method receives an instance of WaitGroup in order to block `newBaseWorkerPool` method until all workers are available. 14 15 At the end, the worker will be killed if task queue they monitored is closed. 16 17 ### Why multi-queue structure? 18 19 In general, the worker pool has a queue only. **`baseWokerPool`** of gost, however, has multiple queues. Why? We found that multi-queue structure could improve performance greatly, especially in the case of using large number of goroutines. 20 21 There are some of benchmark results for your reference. The environment settings are: 22 23 - MacBook Air 2020 with M1 Chip 24 - Memory: 16GB 25 - Golang: go1.16.6 darwin/arm64 26 - @workers: 700 27 - Tasks: CPUTask, IOTask and RandomTask 28 29 **TaskPool(baseWorkerPool based)** 30 31 ``` 32 BenchmarkTaskPool_CPUTask/AddTaskBalance-8 138986 14779 ns/op 33 BenchmarkTaskPool_IOTask/AddTaskBalance-8 2872380 440.2 ns/op 34 BenchmarkTaskPool_RandomTask/AddTaskBalance-8 2365293 530.0 ns/op 35 ``` 36 37 **[WorkerPool](https://github.com/gammazero/workerpool)** 38 39 ``` 40 BenchmarkWorkerPool_CPUTask/Submit-8 70400 17939 ns/op 41 BenchmarkWorkerPool_IOTask/Submit-8 1000000 1011 ns/op 42 BenchmarkWorkerPool_RandomTask/Submit-8 1858268 645.5 ns/op 43 ``` 44 45 **WorkerPool with single queue** 46 47 ``` 48 BenchmarkConnectionPool/CPUTask-8 1844893 16738 ns/op 49 BenchmarkConnectionPool/IOTask-8 1000000 16047 ns/op 50 BenchmarkConnectionPool/RandomTask-8 1000000 1143 ns/op 51 ``` 52 53 ## ConnectionPool 54 55 ConnectionPool is a pool designed for managing connection based on baseWorkerPool. The pool will reject new tasks insertion if reaches the limitation. 56 57 When a new task is arriving, the task will be put into a queue using Round-Robin algorithm at the first time. If failed, the task will be put to a random queue within `len(p.taskQueues)/2` times. If all attempts are failed, it means the pool reaches the limitation, and the task will be rejected eventually. 58