├── .gitignore ├── LICENSE ├── README.md ├── easy_go_pool ├── go_pool.go ├── go_pool_benchmark_test.go └── go_pool_test.go ├── go_pool ├── go_pool.go ├── go_pool_benchmark_test.go └── go_pool_test.go ├── images ├── 1.png ├── 2.png ├── 3.png ├── 4.jpg └── 5.png ├── internal └── job.go └── pool.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | http_* 3 | *.idea/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##### easy_go_pool 特性 2 | ``` 3 | 1.动态扩容至指定size 4 | 2.size可异步改变:AdjustSize(workNum uint16) 5 | 3.可异步关闭也可同步等待任务完成后关闭 6 | 适合对任务处理时间短任务量有限的工作进行处理 7 | ``` 8 | ##### go_pool 特性 9 | ``` 10 | 1.动态扩容至指定size 11 | 2.size可异步改变:AdjustSize(workNum uint16) 12 | 3.当任务量小于需要时会按设置的间隔时间动态缩容 13 | 4.可异步关闭也可同步等待任务完成后关闭 14 | 适合对任务处理时间较长任务量不确定的工作进行处理 15 | ``` 16 | ##### 示例代码 17 | ```go 18 | package main 19 | 20 | import ( 21 | epool "github.com/zhaomin1993/pool/easy_go_pool" 22 | "log" 23 | "math/rand" 24 | "time" 25 | ) 26 | 27 | type Score struct { 28 | Num int 29 | } 30 | 31 | func (s Score) Do() { 32 | if s.Num%10000 == 0 { 33 | log.Println("num:", s.Num) 34 | } 35 | if s.Num%2 == 0 { 36 | panic(s.Num) 37 | } 38 | time.Sleep(time.Millisecond * 100) 39 | } 40 | 41 | func main() { 42 | //创建协程池 43 | pool := epool.NewWorkerPool(1000, 1100) 44 | defer pool.Close() //关闭协程池 45 | pool.OnPanic(func(msg interface{}) { 46 | //log.Println("error:", msg) 47 | }) 48 | datanum := 100 * 100 * 10 49 | for i := 1; i <= datanum; i++ { 50 | //接收任务 51 | if err := pool.Accept(Score{Num: i}); err != nil { 52 | log.Println("err:\t", err) 53 | break 54 | } 55 | if i%10000 == 0 { 56 | log.Println("send num:", i) 57 | randNum := rand.Intn(10) + 1000 58 | //调整协程池大小 59 | pool.AdjustSize(uint16(randNum)) 60 | } 61 | } 62 | } 63 | ``` 64 | 65 | ##### benchmark 测试 66 | ![1.png](https://raw.githubusercontent.com/zhaomin1993/pool/master/images/1.png) 67 | ![2.png](https://raw.githubusercontent.com/zhaomin1993/pool/master/images/2.png) 68 | ![3.png](https://raw.githubusercontent.com/zhaomin1993/pool/master/images/3.png) 69 | ![4.jpg](https://raw.githubusercontent.com/zhaomin1993/pool/master/images/4.jpg) 70 | ![5.png](https://raw.githubusercontent.com/zhaomin1993/pool/master/images/5.png) -------------------------------------------------------------------------------- /easy_go_pool/go_pool.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "errors" 5 | "sync" 6 | 7 | "github.com/zhaomin1993/pool" 8 | "github.com/zhaomin1993/pool/internal" 9 | ) 10 | 11 | // worker 工人 12 | type worker struct { 13 | jobQueue chan internal.Job 14 | stop chan struct{} 15 | } 16 | 17 | // newWorker 新建一个工人 18 | func newWorker() *worker { 19 | return &worker{jobQueue: make(chan internal.Job), stop: make(chan struct{})} 20 | } 21 | 22 | // run 工人开始工作 23 | func (w *worker) run(wq chan<- *worker, onPanic func(msg interface{})) { 24 | go func() { 25 | defer func() { 26 | if r := recover(); r != nil { 27 | w.run(wq, onPanic) 28 | wq <- w 29 | if onPanic != nil { 30 | onPanic(r) 31 | } 32 | } 33 | }() 34 | for { 35 | select { 36 | case job := <-w.jobQueue: 37 | job.Do() 38 | wq <- w 39 | case <-w.stop: 40 | return 41 | } 42 | } 43 | }() 44 | } 45 | 46 | // close 关闭并回收工人 47 | func (w *worker) close() { 48 | w.stop <- struct{}{} 49 | close(w.stop) 50 | close(w.jobQueue) 51 | } 52 | 53 | // workerPool 工厂 54 | type workerPool struct { 55 | closed bool 56 | maxNum uint16 57 | aliveNum uint16 58 | workerNum uint16 59 | mux sync.RWMutex 60 | closeOnce sync.Once 61 | workerQueue chan *worker 62 | onPanic func(msg interface{}) 63 | } 64 | 65 | // NewWorkerPool 创建工厂 66 | func NewWorkerPool(workerNum, maxNum uint16) pool.Pool { 67 | if workerNum > maxNum { 68 | workerNum = maxNum 69 | } 70 | return &workerPool{ 71 | maxNum: maxNum, 72 | workerNum: workerNum, 73 | mux: sync.RWMutex{}, 74 | closeOnce: sync.Once{}, 75 | workerQueue: make(chan *worker, maxNum), 76 | } 77 | } 78 | 79 | // OnPanic 设置工作中出问题时的处理方法 80 | func (wp *workerPool) OnPanic(onPanic func(msg interface{})) { 81 | wp.onPanic = onPanic 82 | } 83 | 84 | // Accept 工厂接收工作任务 85 | func (wp *workerPool) Accept(job internal.Job) (err error) { 86 | if job == nil { 87 | err = errors.New("job can not be nil") 88 | return 89 | } 90 | wp.mux.Lock() 91 | select { 92 | case worker := <-wp.workerQueue: 93 | wp.mux.Unlock() 94 | if worker != nil { 95 | worker.jobQueue <- job 96 | } else { 97 | err = errors.New("worker pool has been closed") 98 | } 99 | default: 100 | var worker *worker 101 | if wp.aliveNum == wp.workerNum { 102 | wp.mux.Unlock() 103 | worker = <-wp.workerQueue 104 | } else if wp.aliveNum < wp.workerNum { 105 | wp.aliveNum++ 106 | wp.mux.Unlock() 107 | worker = newWorker() 108 | worker.run(wp.workerQueue, wp.onPanic) 109 | } else { 110 | wp.mux.Unlock() 111 | panic("worker number less than alive number") 112 | } 113 | if worker != nil { 114 | worker.jobQueue <- job 115 | } else { 116 | err = errors.New("worker pool has been closed") 117 | } 118 | } 119 | return 120 | } 121 | 122 | // Len 工厂活跃工人数 123 | func (wp *workerPool) Len() uint16 { 124 | wp.mux.RLock() 125 | num := wp.aliveNum 126 | wp.mux.RUnlock() 127 | return num 128 | } 129 | 130 | // AdjustSize 调整工厂工人数量 131 | func (wp *workerPool) AdjustSize(workNum uint16) { 132 | wp.mux.Lock() 133 | if wp.closed { 134 | wp.mux.Unlock() 135 | return 136 | } 137 | if workNum > wp.maxNum { 138 | workNum = wp.maxNum 139 | } 140 | oldNum := wp.workerNum 141 | wp.workerNum = workNum 142 | if oldNum == 0 && workNum > 0 { 143 | wp.aliveNum++ 144 | worker := newWorker() 145 | worker.run(wp.workerQueue, wp.onPanic) 146 | wp.workerQueue <- worker 147 | } 148 | for workNum < wp.aliveNum { 149 | wp.aliveNum-- 150 | worker := <-wp.workerQueue 151 | worker.close() 152 | } 153 | wp.mux.Unlock() 154 | } 155 | 156 | // Pause 工厂暂停工作 157 | func (wp *workerPool) Pause() { 158 | wp.AdjustSize(0) 159 | } 160 | 161 | // Continue 工厂继续工作 162 | func (wp *workerPool) Continue(num uint16) { 163 | wp.AdjustSize(num) 164 | } 165 | 166 | // Close 关闭并回收工厂 167 | func (wp *workerPool) Close() { 168 | wp.closeOnce.Do(func() { 169 | wp.mux.Lock() 170 | wp.closed = true 171 | wp.workerNum = 0 172 | wp.maxNum = 0 173 | for 0 < wp.aliveNum { 174 | wp.aliveNum-- 175 | worker := <-wp.workerQueue 176 | worker.close() 177 | } 178 | close(wp.workerQueue) 179 | wp.mux.Unlock() 180 | }) 181 | } 182 | -------------------------------------------------------------------------------- /easy_go_pool/go_pool_benchmark_test.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "github.com/panjf2000/ants" 5 | "sync" 6 | "sync/atomic" 7 | "testing" 8 | ) 9 | 10 | const ( 11 | RunTimes = 1000000 12 | BenchParam = 10 13 | BenchGoSize = 20000 14 | ) 15 | 16 | type obj int32 17 | 18 | func (this obj) Do() { 19 | myFunc(int32(this)) 20 | } 21 | 22 | var sum int32 23 | 24 | func myFunc(i interface{}) { 25 | n := i.(int32) 26 | atomic.AddInt32(&sum, n) 27 | // fmt.Printf("run with %d\n", n) 28 | } 29 | 30 | //go test -bench=BenchmarkGoroutines -benchmem=true -run=none 31 | //BenchmarkGoroutines-8 1 3276239400 ns/op 537844448 B/op 1997684 allocs/op 32 | func BenchmarkGoroutines(b *testing.B) { 33 | var wg sync.WaitGroup 34 | for i := 0; i < b.N; i++ { 35 | wg.Add(RunTimes) 36 | for j := 0; j < RunTimes; j++ { 37 | go func() { 38 | myFunc(int32(j)) 39 | wg.Done() 40 | }() 41 | } 42 | wg.Wait() 43 | } 44 | } 45 | 46 | //go test -bench=BenchmarkSemaphore -benchmem=true -run=none 47 | //BenchmarkSemaphore-8 2 651259850 ns/op 64292344 B/op 1001274 allocs/op 48 | func BenchmarkSemaphore(b *testing.B) { 49 | var wg sync.WaitGroup 50 | sema := make(chan struct{}, BenchGoSize) 51 | 52 | for i := 0; i < b.N; i++ { 53 | wg.Add(RunTimes) 54 | for j := 0; j < RunTimes; j++ { 55 | sema <- struct{}{} 56 | go func() { 57 | myFunc(int32(j)) 58 | <-sema 59 | wg.Done() 60 | }() 61 | } 62 | wg.Wait() 63 | } 64 | } 65 | 66 | //go test -bench=BenchmarkAntsPool -benchmem=true -run=none 67 | //BenchmarkAntsPool-8 2 587444950 ns/op 20669048 B/op 1059622 allocs/op 68 | func BenchmarkAntsPool(b *testing.B) { 69 | var wg sync.WaitGroup 70 | p, _ := ants.NewPoolWithFunc(BenchGoSize, func(i interface{}) { 71 | myFunc(i) 72 | wg.Done() 73 | }) 74 | defer p.Release() 75 | 76 | b.StartTimer() 77 | for i := 0; i < b.N; i++ { 78 | wg.Add(RunTimes) 79 | for j := 0; j < RunTimes; j++ { 80 | p.Invoke(int32(j)) 81 | } 82 | wg.Wait() 83 | } 84 | b.StopTimer() 85 | } 86 | 87 | //go test -bench=BenchmarkGoPool -benchmem=true -run=none 88 | //BenchmarkGoPool-8 2 564994050 ns/op 3521408 B/op 45269 allocs/op 89 | func BenchmarkGoPool(b *testing.B) { 90 | p := NewWorkerPool(uint16(BenchGoSize), uint16(BenchGoSize)) 91 | defer p.Close() 92 | b.StartTimer() 93 | for i := 0; i < b.N; i++ { 94 | p.AdjustSize(uint16(BenchGoSize)) 95 | for j := 0; j < RunTimes; j++ { 96 | _ = p.Accept(obj(j)) 97 | } 98 | p.AdjustSize(0) 99 | } 100 | b.StopTimer() 101 | } 102 | 103 | //BenchmarkGoroutinesThroughput-8 4 264044400 ns/op 63998256 B/op 999972 allocs/op 104 | func BenchmarkGoroutinesThroughput(b *testing.B) { 105 | for i := 0; i < b.N; i++ { 106 | for j := 0; j < RunTimes; j++ { 107 | go myFunc(int32(j)) 108 | } 109 | } 110 | } 111 | 112 | //BenchmarkSemaphoreThroughput-8 2 631312300 ns/op 64034400 B/op 1000145 allocs/op 113 | func BenchmarkSemaphoreThroughput(b *testing.B) { 114 | sema := make(chan struct{}, BenchGoSize) 115 | for i := 0; i < b.N; i++ { 116 | for j := 0; j < RunTimes; j++ { 117 | sema <- struct{}{} 118 | go func() { 119 | myFunc(int32(j)) 120 | <-sema 121 | }() 122 | } 123 | } 124 | } 125 | 126 | //BenchmarkGoPoolThroughput-8 2 562498700 ns/op 3385600 B/op 43775 allocs/op 127 | func BenchmarkGoPoolThroughput(b *testing.B) { 128 | p := NewWorkerPool(uint16(BenchGoSize), uint16(BenchGoSize)) 129 | defer p.Close() 130 | b.StartTimer() 131 | for i := 0; i < b.N; i++ { 132 | for j := 0; j < RunTimes; j++ { 133 | _ = p.Accept(obj(j)) 134 | } 135 | } 136 | b.StopTimer() 137 | } 138 | 139 | //BenchmarkAntsPoolThroughput-8 2 552554100 ns/op 2588784 B/op 41928 allocs/op 140 | func BenchmarkAntsPoolThroughput(b *testing.B) { 141 | p, _ := ants.NewPoolWithFunc(BenchGoSize, func(i interface{}) { 142 | myFunc(i) 143 | }) 144 | defer p.Release() 145 | b.StartTimer() 146 | for i := 0; i < b.N; i++ { 147 | for j := 0; j < RunTimes; j++ { 148 | _ = p.Invoke(int32(j)) 149 | } 150 | } 151 | b.StopTimer() 152 | } 153 | -------------------------------------------------------------------------------- /easy_go_pool/go_pool_test.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "math/rand" 7 | "runtime" 8 | "testing" 9 | "time" 10 | ) 11 | 12 | type Score struct { 13 | Num int 14 | } 15 | 16 | func (s *Score) Do() { 17 | if s.Num%10000 == 0 { 18 | log.Println("num:", s.Num) 19 | } 20 | if s.Num%2 == 0 { 21 | panic(s.Num) 22 | } 23 | time.Sleep(time.Millisecond * 10) 24 | } 25 | 26 | //go test -v -test.run TestWorkerPool_Run 27 | func TestWorkerPool_Run(t *testing.T) { 28 | log.Println("runtime.NumGoroutine() :", runtime.NumGoroutine()) 29 | p := NewWorkerPool(1000, 1100) 30 | p.OnPanic(func(msg interface{}) { 31 | //log.Println("error:", msg) 32 | }) 33 | datanum := 100 * 100 * 100 34 | go func() { 35 | defer func() { 36 | if r := recover(); r != nil { 37 | log.Println(r) 38 | } 39 | }() 40 | start := time.Now() 41 | for i := 1; i <= datanum; i++ { 42 | sc := &Score{Num: i} 43 | if err := p.Accept(sc); err != nil { 44 | fmt.Println("err:\t", err) 45 | break 46 | } 47 | if i%10000 == 0 { 48 | log.Println("send num:", i) 49 | } 50 | randNum := rand.Intn(10) + 1000 51 | p.AdjustSize(uint16(randNum)) 52 | } 53 | log.Println("start wait.....") 54 | p.Close() 55 | log.Printf("cost time:%s\n", time.Since(start).String()) 56 | log.Println("stop over.....") 57 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 58 | }() 59 | for { 60 | time.Sleep(1 * time.Second) 61 | log.Printf("runtime.NumGoroutine() :%d\n", runtime.NumGoroutine()) 62 | } 63 | } 64 | 65 | //go test -v -test.run TestWorkerPool_Close 66 | func TestWorkerPool_Close(t *testing.T) { 67 | log.Println("runtime.NumGoroutine() :", runtime.NumGoroutine()) 68 | p := NewWorkerPool(1000, 1100) 69 | p.OnPanic(func(msg interface{}) { 70 | //log.Println("error:", msg) 71 | }) 72 | datanum := 100 * 100 * 100 73 | go func() { 74 | defer func() { 75 | if r := recover(); r != nil { 76 | log.Println(r) 77 | } 78 | }() 79 | for i := 1; i <= datanum; i++ { 80 | sc := &Score{Num: i} 81 | if err := p.Accept(sc); err != nil { 82 | fmt.Println("err:\t", err) 83 | break 84 | } 85 | if i%10000 == 0 { 86 | log.Println("send num:", i) 87 | } 88 | } 89 | log.Println("start wait.....") 90 | p.Close() 91 | log.Println("stop over.....") 92 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 93 | }() 94 | go func() { 95 | time.Sleep(time.Second * 3) 96 | p.Close() 97 | }() 98 | go func() { 99 | rand.Seed(time.Now().Unix()) 100 | for { 101 | time.Sleep(time.Second) 102 | randNum := rand.Intn(10) + 1000 103 | p.AdjustSize(uint16(randNum)) 104 | } 105 | }() 106 | for { 107 | time.Sleep(1 * time.Second) 108 | log.Printf("runtime.NumGoroutine() :%d\n", runtime.NumGoroutine()) 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /go_pool/go_pool.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "errors" 5 | "sync" 6 | "time" 7 | 8 | "github.com/zhaomin1993/pool" 9 | "github.com/zhaomin1993/pool/internal" 10 | ) 11 | 12 | // --------------------------- Worker --------------------- 13 | type worker struct { 14 | jobQueue chan internal.Job 15 | stop chan struct{} 16 | } 17 | 18 | // run 创建一个工人 19 | func newWorker() *worker { 20 | return &worker{jobQueue: make(chan internal.Job), stop: make(chan struct{})} 21 | } 22 | 23 | // run 工人进入工作状态 24 | func (w *worker) run(wq chan<- *worker, onPanic func(msg interface{})) { 25 | go func() { 26 | defer func() { 27 | if r := recover(); r != nil { 28 | w.run(wq, onPanic) 29 | wq <- w 30 | if onPanic != nil { 31 | onPanic(r) 32 | } 33 | } 34 | }() 35 | for { 36 | select { 37 | case job := <-w.jobQueue: 38 | job.Do() 39 | wq <- w 40 | case <-w.stop: 41 | return 42 | } 43 | } 44 | }() 45 | } 46 | 47 | func (w *worker) stopRun() { 48 | w.stop <- struct{}{} 49 | } 50 | 51 | // close 回收工人 52 | func (w *worker) close() { 53 | w.stop <- struct{}{} 54 | close(w.stop) 55 | close(w.jobQueue) 56 | } 57 | 58 | // --------------------------- WorkerPool --------------------- 59 | type workerPool struct { 60 | closed bool 61 | maxSize uint16 62 | aliveNum uint16 63 | workerNum uint16 64 | workerSize uint16 65 | blockAccept uint16 66 | workers sync.Pool 67 | mux sync.RWMutex 68 | closeOnce sync.Once 69 | workerQueue chan *worker 70 | stopAuto chan struct{} 71 | onPanic func(msg interface{}) 72 | } 73 | 74 | // NewWorkerPool 创建协程池 75 | func NewWorkerPool(workerNum, maxSize uint16, interval time.Duration) pool.Pool { 76 | wp := &workerPool{ 77 | maxSize: maxSize, 78 | workerNum: workerNum, 79 | workerSize: workerNum, 80 | mux: sync.RWMutex{}, 81 | closeOnce: sync.Once{}, 82 | workerQueue: make(chan *worker, maxSize), 83 | stopAuto: make(chan struct{}), 84 | workers: sync.Pool{}, 85 | } 86 | wp.workers.New = func() interface{} { 87 | return newWorker() 88 | } 89 | wp.autoCutCap(interval) 90 | return wp 91 | } 92 | 93 | func (wp *workerPool) OnPanic(onPanic func(msg interface{})) { 94 | wp.onPanic = onPanic 95 | } 96 | 97 | // Accept 协程池接收任务 98 | func (wp *workerPool) Accept(job internal.Job) (err error) { 99 | if job == nil { 100 | err = errors.New("job can not be nil") 101 | return 102 | } 103 | wp.mux.Lock() 104 | select { 105 | case worker := <-wp.workerQueue: 106 | wp.mux.Unlock() 107 | if worker != nil { 108 | worker.jobQueue <- job 109 | } else { 110 | err = errors.New("worker pool has been closed") 111 | } 112 | default: 113 | switch { 114 | case wp.aliveNum == wp.workerNum: 115 | if wp.workerNum == wp.workerSize { 116 | if wp.workerSize == 0 { 117 | wp.blockAccept++ 118 | } 119 | wp.mux.Unlock() 120 | worker := <-wp.workerQueue 121 | if worker != nil { 122 | worker.jobQueue <- job 123 | } else { 124 | err = errors.New("worker pool has been closed") 125 | } 126 | return 127 | } 128 | wp.workerNum = wp.workerSize 129 | wp.mux.Unlock() 130 | err = wp.Accept(job) 131 | case wp.aliveNum < wp.workerNum: 132 | wp.aliveNum++ 133 | wp.mux.Unlock() 134 | worker := wp.workers.Get().(*worker) 135 | worker.run(wp.workerQueue, wp.onPanic) 136 | worker.jobQueue <- job 137 | default: 138 | wp.mux.Unlock() 139 | panic("worker number less than alive number") 140 | } 141 | } 142 | return 143 | } 144 | 145 | // Len 获取协程数 146 | func (wp *workerPool) Len() uint16 { 147 | wp.mux.RLock() 148 | num := wp.aliveNum 149 | wp.mux.RUnlock() 150 | return num 151 | } 152 | 153 | // AdjustSize 调整协程池大小 154 | func (wp *workerPool) AdjustSize(workSize uint16) { 155 | wp.mux.Lock() 156 | if wp.closed { 157 | wp.mux.Unlock() 158 | return 159 | } 160 | if workSize > wp.maxSize { 161 | workSize = wp.maxSize 162 | } 163 | if wp.workerNum > workSize { 164 | wp.workerNum = workSize 165 | } 166 | oldSize := wp.workerSize 167 | wp.workerSize = workSize 168 | if oldSize == 0 && workSize > 0 && wp.blockAccept > 0 { 169 | times := wp.blockAccept 170 | if workSize < wp.blockAccept { 171 | times = workSize 172 | } 173 | wp.blockAccept = 0 174 | for i := uint16(0); i < times; i++ { 175 | wp.aliveNum++ 176 | wp.workerNum++ 177 | worker := wp.workers.Get().(*worker) 178 | worker.run(wp.workerQueue, wp.onPanic) 179 | wp.workerQueue <- worker 180 | } 181 | } 182 | for workSize < wp.aliveNum { 183 | wp.aliveNum-- 184 | worker := <-wp.workerQueue 185 | worker.stopRun() 186 | wp.workers.Put(worker) 187 | } 188 | wp.mux.Unlock() 189 | } 190 | 191 | // Pause 暂停 192 | func (wp *workerPool) Pause() { 193 | wp.AdjustSize(0) 194 | } 195 | 196 | // Continue 继续 197 | func (wp *workerPool) Continue(num uint16) { 198 | wp.AdjustSize(num) 199 | } 200 | 201 | // Close 关闭协程池 202 | func (wp *workerPool) Close() { 203 | wp.closeOnce.Do(func() { 204 | wp.mux.Lock() 205 | wp.closed = true 206 | close(wp.stopAuto) 207 | wp.workerNum = 0 208 | wp.workerSize = 0 209 | wp.maxSize = 0 210 | for 0 < wp.aliveNum { 211 | wp.aliveNum-- 212 | worker := <-wp.workerQueue 213 | worker.close() 214 | } 215 | close(wp.workerQueue) 216 | wp.mux.Unlock() 217 | }) 218 | } 219 | 220 | // autoCutCap 自动缩容 221 | func (wp *workerPool) autoCutCap(interval time.Duration) { 222 | go func() { 223 | ticker := time.NewTicker(interval) 224 | for { 225 | select { 226 | case <-ticker.C: 227 | wp.mux.Lock() 228 | length := len(wp.workerQueue) 229 | if 1 < length && uint16(length) <= wp.aliveNum { 230 | num := wp.aliveNum - uint16(length>>1) 231 | wp.workerNum = num 232 | for num < wp.aliveNum { 233 | wp.aliveNum-- 234 | worker := <-wp.workerQueue 235 | worker.stopRun() 236 | wp.workers.Put(worker) 237 | } 238 | } 239 | wp.mux.Unlock() 240 | case <-wp.stopAuto: 241 | ticker.Stop() 242 | return 243 | } 244 | } 245 | }() 246 | } 247 | -------------------------------------------------------------------------------- /go_pool/go_pool_benchmark_test.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "github.com/panjf2000/ants" 5 | "sync" 6 | "testing" 7 | "time" 8 | ) 9 | 10 | const ( 11 | RunTimes = 1000000 12 | BenchParam = 10 13 | BenchGoSize = 20000 14 | DefaultExpiredTime = 10 * time.Second 15 | ) 16 | 17 | type obj struct{} 18 | 19 | func (obj) Do() { 20 | demoFunc() 21 | } 22 | 23 | func demoFunc() { 24 | time.Sleep(time.Duration(BenchParam) * time.Millisecond) 25 | } 26 | 27 | //go test -bench=BenchmarkGoroutines -benchmem=true -run=none 28 | //BenchmarkGoroutines-8 1 3276239400 ns/op 537844448 B/op 1997684 allocs/op 29 | func BenchmarkGoroutines(b *testing.B) { 30 | var wg sync.WaitGroup 31 | for i := 0; i < b.N; i++ { 32 | wg.Add(RunTimes) 33 | for j := 0; j < RunTimes; j++ { 34 | go func() { 35 | demoFunc() 36 | wg.Done() 37 | }() 38 | } 39 | wg.Wait() 40 | } 41 | } 42 | 43 | //go test -bench=BenchmarkSemaphore -benchmem=true -run=none 44 | //BenchmarkSemaphore-8 2 651259850 ns/op 64292344 B/op 1001274 allocs/op 45 | func BenchmarkSemaphore(b *testing.B) { 46 | var wg sync.WaitGroup 47 | sema := make(chan struct{}, BenchGoSize) 48 | 49 | for i := 0; i < b.N; i++ { 50 | wg.Add(RunTimes) 51 | for j := 0; j < RunTimes; j++ { 52 | sema <- struct{}{} 53 | go func() { 54 | demoFunc() 55 | <-sema 56 | wg.Done() 57 | }() 58 | } 59 | wg.Wait() 60 | } 61 | } 62 | 63 | //go test -bench=BenchmarkAntsPool -benchmem=true -run=none 64 | //BenchmarkAntsPool-8 2 593923650 ns/op 21303400 B/op 1066102 allocs/op 65 | func BenchmarkAntsPool(b *testing.B) { 66 | var wg sync.WaitGroup 67 | p, _ := ants.NewPool(BenchGoSize, ants.WithExpiryDuration(DefaultExpiredTime)) 68 | defer p.Release() 69 | 70 | b.StartTimer() 71 | for i := 0; i < b.N; i++ { 72 | wg.Add(RunTimes) 73 | for j := 0; j < RunTimes; j++ { 74 | _ = p.Submit(func() { 75 | demoFunc() 76 | wg.Done() 77 | }) 78 | } 79 | wg.Wait() 80 | } 81 | b.StopTimer() 82 | } 83 | 84 | //go test -bench=BenchmarkGoPool -benchmem=true -run=none 85 | //BenchmarkGoPool-8 2 571007850 ns/op 3657388 B/op 46307 allocs/op 86 | func BenchmarkGoPool(b *testing.B) { 87 | p := NewWorkerPool(uint16(BenchGoSize), uint16(BenchGoSize), DefaultExpiredTime) 88 | 89 | b.StartTimer() 90 | for i := 0; i < b.N; i++ { 91 | p.AdjustSize(uint16(BenchGoSize)) 92 | for j := 0; j < RunTimes; j++ { 93 | _ = p.Accept(obj{}) 94 | } 95 | p.AdjustSize(0) 96 | } 97 | p.Close() 98 | b.StopTimer() 99 | } 100 | 101 | //BenchmarkGoroutinesThroughput-8 4 264044400 ns/op 63998256 B/op 999972 allocs/op 102 | func BenchmarkGoroutinesThroughput(b *testing.B) { 103 | for i := 0; i < b.N; i++ { 104 | for j := 0; j < RunTimes; j++ { 105 | go demoFunc() 106 | } 107 | } 108 | } 109 | 110 | //BenchmarkSemaphoreThroughput-8 2 631312300 ns/op 64034400 B/op 1000145 allocs/op 111 | func BenchmarkSemaphoreThroughput(b *testing.B) { 112 | sema := make(chan struct{}, BenchGoSize) 113 | for i := 0; i < b.N; i++ { 114 | for j := 0; j < RunTimes; j++ { 115 | sema <- struct{}{} 116 | go func() { 117 | demoFunc() 118 | <-sema 119 | }() 120 | } 121 | } 122 | } 123 | 124 | //BenchmarkGoPoolThroughput-8 2 581976150 ns/op 3570988 B/op 46128 allocs/op 125 | func BenchmarkGoPoolThroughput(b *testing.B) { 126 | p := NewWorkerPool(uint16(BenchGoSize), uint16(BenchGoSize), DefaultExpiredTime) 127 | defer p.Close() 128 | b.StartTimer() 129 | for i := 0; i < b.N; i++ { 130 | for j := 0; j < RunTimes; j++ { 131 | _ = p.Accept(obj{}) 132 | } 133 | } 134 | b.StopTimer() 135 | } 136 | 137 | //BenchmarkAntsPoolThroughput-8 2 550545450 ns/op 2504716 B/op 41251 allocs/op 138 | func BenchmarkAntsPoolThroughput(b *testing.B) { 139 | p, _ := ants.NewPool(BenchGoSize, ants.WithExpiryDuration(DefaultExpiredTime)) 140 | defer p.Release() 141 | b.StartTimer() 142 | for i := 0; i < b.N; i++ { 143 | for j := 0; j < RunTimes; j++ { 144 | _ = p.Submit(demoFunc) 145 | } 146 | } 147 | b.StopTimer() 148 | } 149 | -------------------------------------------------------------------------------- /go_pool/go_pool_test.go: -------------------------------------------------------------------------------- 1 | package go_pool 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "math/rand" 7 | "runtime" 8 | "testing" 9 | "time" 10 | ) 11 | 12 | type Score struct { 13 | Num int 14 | } 15 | 16 | func (s *Score) Do() { 17 | if s.Num%10000 == 0 { 18 | log.Println("num:", s.Num) 19 | } 20 | if s.Num%2 == 0 { 21 | panic(s.Num) 22 | } 23 | time.Sleep(time.Millisecond * 100) 24 | } 25 | 26 | //go test -v -test.run TestWorkerPool_Run 27 | func TestWorkerPool_Run(t *testing.T) { 28 | log.Println("runtime.NumGoroutine() :", runtime.NumGoroutine()) 29 | p := NewWorkerPool(1000, 1100, time.Second*5) 30 | p.OnPanic(func(msg interface{}) { 31 | //log.Println("error:", msg) 32 | }) 33 | datanum := 100 * 100 * 100 34 | go func() { 35 | defer func() { 36 | if r := recover(); r != nil { 37 | log.Println(r) 38 | } 39 | }() 40 | start := time.Now() 41 | for i := 1; i <= datanum; i++ { 42 | sc := &Score{Num: i} 43 | if err := p.Accept(sc); err != nil { 44 | fmt.Println("err:\t", err) 45 | break 46 | } 47 | if i%10000 == 0 { 48 | log.Println("send num:", i) 49 | } 50 | randNum := rand.Intn(10) + 1000 51 | p.AdjustSize(uint16(randNum)) 52 | } 53 | log.Println("start wait.....") 54 | //p.Close() 55 | log.Printf("cost time:%s\n", time.Since(start).String()) 56 | log.Println("stop over.....") 57 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 58 | }() 59 | go func() { 60 | time.Sleep(time.Second * 62) 61 | log.Println("start again.....------------------------------") 62 | for i := 1; i <= datanum; i++ { 63 | sc := &Score{Num: i} 64 | if err := p.Accept(sc); err != nil { 65 | fmt.Println("err:\t", err) 66 | break 67 | } 68 | if i%10000 == 0 { 69 | log.Println("send num:", i) 70 | } 71 | randNum := rand.Intn(10) + 1000 72 | p.AdjustSize(uint16(randNum)) 73 | } 74 | p.Close() 75 | for i := 1; i <= datanum; i++ { 76 | sc := &Score{Num: i} 77 | if err := p.Accept(sc); err != nil { 78 | fmt.Println("err:\t", err) 79 | break 80 | } 81 | if i%10000 == 0 { 82 | log.Println("send num:", i) 83 | } 84 | randNum := rand.Intn(10) + 1000 85 | p.AdjustSize(uint16(randNum)) 86 | } 87 | }() 88 | for { 89 | time.Sleep(1 * time.Second) 90 | log.Printf("runtime.NumGoroutine() :%d\n", runtime.NumGoroutine()) 91 | } 92 | } 93 | 94 | //go test -v -test.run TestWorkerPool_Close 95 | func TestWorkerPool_Close(t *testing.T) { 96 | log.Println("runtime.NumGoroutine() :", runtime.NumGoroutine()) 97 | p := NewWorkerPool(1000, 1100, time.Second) 98 | p.OnPanic(func(msg interface{}) { 99 | //log.Println("error:", msg) 100 | }) 101 | datanum := 100 * 100 * 100 102 | go func() { 103 | defer func() { 104 | if r := recover(); r != nil { 105 | log.Println(r) 106 | } 107 | }() 108 | for i := 1; i <= datanum; i++ { 109 | sc := &Score{Num: i} 110 | if err := p.Accept(sc); err != nil { 111 | fmt.Println("err:\t", err) 112 | break 113 | } 114 | } 115 | log.Println("start wait.....") 116 | p.Close() 117 | log.Println("stop over.....") 118 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 119 | }() 120 | go func() { 121 | rand.Seed(time.Now().Unix()) 122 | randNum := rand.Intn(3) + 1 123 | time.Sleep(time.Second * time.Duration(randNum)) 124 | p.Close() 125 | }() 126 | go func() { 127 | for { 128 | time.Sleep(time.Second) 129 | randNum := rand.Intn(10) + 1000 130 | p.AdjustSize(uint16(randNum)) 131 | } 132 | }() 133 | for { 134 | time.Sleep(1 * time.Second) 135 | log.Printf("runtime.NumGoroutine() :%d\n", runtime.NumGoroutine()) 136 | } 137 | } 138 | 139 | //go test -v -test.run TestWorkerPool_AdjustSize 140 | func TestWorkerPool_AdjustSize(t *testing.T) { 141 | log.Println("runtime.NumGoroutine() :", runtime.NumGoroutine()) 142 | p := NewWorkerPool(1000, 1100, time.Second) 143 | p.OnPanic(func(msg interface{}) { 144 | //log.Println("error:", msg) 145 | }) 146 | datanum := 100 * 100 * 100 147 | go func() { 148 | defer func() { 149 | if r := recover(); r != nil { 150 | log.Println(r) 151 | } 152 | }() 153 | for i := 1; i <= datanum; i++ { 154 | sc := &Score{Num: i} 155 | if err := p.Accept(sc); err != nil { 156 | fmt.Println("err:\t", err) 157 | break 158 | } 159 | } 160 | log.Println("start wait.....") 161 | p.Close() 162 | log.Println("stop over.....") 163 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 164 | }() 165 | go func() { 166 | defer func() { 167 | if r := recover(); r != nil { 168 | log.Println(r) 169 | } 170 | }() 171 | for i := 1; i <= datanum; i++ { 172 | sc := &Score{Num: i} 173 | if err := p.Accept(sc); err != nil { 174 | fmt.Println("err:\t", err) 175 | break 176 | } 177 | } 178 | log.Println("start wait.....") 179 | p.Close() 180 | log.Println("stop over.....") 181 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 182 | }() 183 | go func() { 184 | defer func() { 185 | if r := recover(); r != nil { 186 | log.Println(r) 187 | } 188 | }() 189 | for i := 1; i <= datanum; i++ { 190 | sc := &Score{Num: i} 191 | if err := p.Accept(sc); err != nil { 192 | fmt.Println("err:\t", err) 193 | break 194 | } 195 | } 196 | log.Println("start wait.....") 197 | p.Close() 198 | log.Println("stop over.....") 199 | log.Println("the last runtime.NumGoroutine() :", runtime.NumGoroutine()) 200 | }() 201 | go func() { 202 | num := 0 203 | for { 204 | time.Sleep(time.Second) 205 | num++ 206 | if num%2 == 0 { 207 | p.AdjustSize(0) 208 | } else { 209 | p.AdjustSize(1000) 210 | } 211 | } 212 | }() 213 | for { 214 | time.Sleep(1 * time.Second) 215 | log.Printf("runtime.NumGoroutine() :%d\n", runtime.NumGoroutine()) 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaomin1993/pool/bc3fc3d7b2523d03e1d4189958afa4ba4b9ad4e5/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaomin1993/pool/bc3fc3d7b2523d03e1d4189958afa4ba4b9ad4e5/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaomin1993/pool/bc3fc3d7b2523d03e1d4189958afa4ba4b9ad4e5/images/3.png -------------------------------------------------------------------------------- /images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaomin1993/pool/bc3fc3d7b2523d03e1d4189958afa4ba4b9ad4e5/images/4.jpg -------------------------------------------------------------------------------- /images/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaomin1993/pool/bc3fc3d7b2523d03e1d4189958afa4ba4b9ad4e5/images/5.png -------------------------------------------------------------------------------- /internal/job.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | // job 工作接口 4 | type Job interface { 5 | Do() //不允许永远阻塞,代码要求高 6 | } 7 | -------------------------------------------------------------------------------- /pool.go: -------------------------------------------------------------------------------- 1 | package pool 2 | 3 | import "github.com/zhaomin1993/pool/internal" 4 | 5 | type Pool interface { 6 | OnPanic(onPanic func(msg interface{})) 7 | Accept(job internal.Job) (err error) 8 | Len() uint16 9 | AdjustSize(workNum uint16) 10 | Pause() 11 | Continue(num uint16) 12 | Close() 13 | } 14 | --------------------------------------------------------------------------------