From 8392dbebfe1de3479fa05ede0cf5b898f4e9a6ac Mon Sep 17 00:00:00 2001 From: andy pan Date: Mon, 2 Jul 2018 14:21:27 +0800 Subject: [PATCH 1/9] update travis-ci branch --- README_ZH.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README_ZH.md b/README_ZH.md index a50e954..afa1290 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -176,7 +176,7 @@ Memory : 8 GB 1867 MHz DDR3 **从该demo测试吞吐性能对比可以看出,使用ants的吞吐性能相较于原生goroutine可以保持在2-6倍的性能压制,而内存消耗则可以达到10-20倍的节省优势。** -[1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop +[1]: https://travis-ci.com/panjf2000/ants.svg?branch=master [2]: https://travis-ci.com/panjf2000/ants [3]: https://godoc.org/github.com/panjf2000/ants?status.svg [4]: https://godoc.org/github.com/panjf2000/ants From 1cb275b04ca0e4aaed2e819a4058551ea818d339 Mon Sep 17 00:00:00 2001 From: andy pan Date: Mon, 2 Jul 2018 14:27:29 +0800 Subject: [PATCH 2/9] close the pool after testing --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6469519..48cecf4 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ There was only the test of `ants` Pool because my computer was crash when it rea **As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.** -[1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop +[1]: https://travis-ci.com/panjf2000/ants.svg?branch=master [2]: https://travis-ci.com/panjf2000/ants [3]: https://godoc.org/github.com/panjf2000/ants?status.svg [4]: https://godoc.org/github.com/panjf2000/ants From 9978fa241404185d139f9b2971e0e8a95550ad47 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 9 Jul 2018 16:32:33 +0800 Subject: [PATCH 3/9] update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7663ea4..17e4f90 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ Package ants implements a fixed goroutine pool for managing and recycling a mass ## Features: - Automatically managing and recycling a massive number of goroutines. -- Friendly interfaces: submitting tasks, getting the number of running goroutines, readjusting capacity of pool dynamically, closing pool. +- Periodically clearing the overdue goroutines. +- Friendly interfaces: submitting tasks, getting the number of running goroutines, readjusting capacity of pool dynamically, closing pool. - Efficient in memory usage and it even achieves higher performance than unlimited goroutines in golang. From b557e74355086136729d67466c7c72d342f2dfa8 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 9 Jul 2018 16:34:44 +0800 Subject: [PATCH 4/9] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 17e4f90..d4e94b2 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Package ants implements a fixed goroutine pool for managing and recycling a mass ## Features: - Automatically managing and recycling a massive number of goroutines. -- Periodically clearing the overdue goroutines. +- Periodically clearing overdue goroutines. - Friendly interfaces: submitting tasks, getting the number of running goroutines, readjusting capacity of pool dynamically, closing pool. - Efficient in memory usage and it even achieves higher performance than unlimited goroutines in golang. From e5ab806f5a6c1c99d43d1e4cad51b3c800070be2 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 9 Jul 2018 16:36:42 +0800 Subject: [PATCH 5/9] update readme --- README_ZH.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README_ZH.md b/README_ZH.md index c1f4ddf..ade5246 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -18,6 +18,7 @@ ## 功能: - 实现了自动调度并发的goroutine,复用goroutine +- 定时清理过期的goroutine,进一步节省资源 - 提供了友好的接口:任务提交、获取运行中的协程数量、动态调整协程池大小 - 资源复用,极大节省内存使用量;在大规模批量并发任务场景下比原生goroutine并发具有更高的性能 From 0bbc2a2ff001be8e0b51d268558031d0a0c5607b Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 22:54:03 +0800 Subject: [PATCH 6/9] Merge branch 'develop' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e5e3b39..cc6d6b7 100644 --- a/README.md +++ b/README.md @@ -186,7 +186,7 @@ There was only the test of `ants` Pool because my computer was crash when it rea [1]: https://travis-ci.com/panjf2000/ants.svg?branch=master [2]: https://travis-ci.com/panjf2000/ants -[3]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg +[3]: https://codecov.io/gh/panjf2000/ants/branch/master/graph/badge.svg [4]: https://codecov.io/gh/panjf2000/ants [5]: https://goreportcard.com/badge/github.com/panjf2000/ants [6]: https://goreportcard.com/report/github.com/panjf2000/ants From cdde5659ba01f57dcf5da1e1ba5eca2be2efbcee Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 16 Jul 2018 12:25:25 +0800 Subject: [PATCH 7/9] add codecov test --- README_ZH.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README_ZH.md b/README_ZH.md index ade5246..0708a7f 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -4,12 +4,11 @@

A goroutine pool for Go

- - [![Build Status][1]][2] -[![godoc for panjf2000/ants][3]][4] +[![codecov][3]][4] [![goreportcard for panjf2000/ants][5]][6] -[![MIT Licence][7]][8] +[![godoc for panjf2000/ants][7]][8] +[![MIT Licence][9]][10] [英文说明页](README.md) | [项目介绍文章传送门](http://blog.taohuawu.club/article/42) @@ -183,9 +182,11 @@ Go1.9 [1]: https://travis-ci.com/panjf2000/ants.svg?branch=master [2]: https://travis-ci.com/panjf2000/ants -[3]: https://godoc.org/github.com/panjf2000/ants?status.svg -[4]: https://godoc.org/github.com/panjf2000/ants +[3]: https://codecov.io/gh/panjf2000/ants/branch/master/graph/badge.svg +[4]: https://codecov.io/gh/panjf2000/ants [5]: https://goreportcard.com/badge/github.com/panjf2000/ants [6]: https://goreportcard.com/report/github.com/panjf2000/ants -[7]: https://badges.frapsoft.com/os/mit/mit.svg?v=103 -[8]: https://opensource.org/licenses/mit-license.php +[7]: https://godoc.org/github.com/panjf2000/ants?status.svg +[8]: https://godoc.org/github.com/panjf2000/ants +[9]: https://badges.frapsoft.com/os/mit/mit.svg?v=103 +[10]: https://opensource.org/licenses/mit-license.php From 117222ee6b3a92a0f7cd661de9d5679bbf06caf2 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Mon, 16 Jul 2018 12:45:03 +0800 Subject: [PATCH 8/9] update codecov test --- ants_benchmark_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index af0ef99..0374d8a 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -44,7 +44,7 @@ const ( const ( RunTimes = 10000000 Param = 100 - AntsSize = 100 + AntsSize = 500 ) func demoFunc() error { From 83042d709562a53973c78901ca5df7e7cddbe677 Mon Sep 17 00:00:00 2001 From: barryz Date: Wed, 25 Jul 2018 22:28:11 +0800 Subject: [PATCH 9/9] pre-allocate the capacity of the worker list --- ants_test.go | 2 +- pool.go | 17 ++++++++++++----- pool_func.go | 17 ++++++++++++----- worker.go | 3 +-- worker_func.go | 3 +-- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/ants_test.go b/ants_test.go index 80cf140..f61efe5 100644 --- a/ants_test.go +++ b/ants_test.go @@ -26,9 +26,9 @@ import ( "runtime" "sync" "testing" + "time" "github.com/panjf2000/ants" - "time" ) var n = 100000 diff --git a/pool.go b/pool.go index aec9573..ac3edcd 100644 --- a/pool.go +++ b/pool.go @@ -129,6 +129,16 @@ func (p *Pool) Running() int { return int(atomic.LoadInt32(&p.running)) } +// IncrRunning increases the number of the currently running goroutines +func (p *Pool) IncrRunning() { + atomic.AddInt32(&p.running, 1) +} + +// DecrRunning decreases the number of the currently running goroutines +func (p *Pool) DecrRunning() { + atomic.AddInt32(&p.running, -1) +} + // Free returns the available goroutines to work func (p *Pool) Free() int { return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running)) @@ -181,11 +191,7 @@ func (p *Pool) getWorker() *Worker { idleWorkers := p.workers n := len(idleWorkers) - 1 if n < 0 { - if p.Running() >= p.Cap() { - waiting = true - } else { - atomic.AddInt32(&p.running, 1) - } + waiting = p.Running() >= p.Cap() } else { <-p.freeSignal w = idleWorkers[n] @@ -209,6 +215,7 @@ func (p *Pool) getWorker() *Worker { task: make(chan f, 1), } w.run() + p.IncrRunning() } return w } diff --git a/pool_func.go b/pool_func.go index c3f20a7..77221e4 100644 --- a/pool_func.go +++ b/pool_func.go @@ -134,6 +134,16 @@ func (p *PoolWithFunc) Running() int { return int(atomic.LoadInt32(&p.running)) } +// IncrRunning increases the number of the currently running goroutines +func (p *PoolWithFunc) IncrRunning() { + atomic.AddInt32(&p.running, 1) +} + +// DecrRunning decreases the number of the currently running goroutines +func (p *PoolWithFunc) DecrRunning() { + atomic.AddInt32(&p.running, -1) +} + // Free returns the available goroutines to work func (p *PoolWithFunc) Free() int { return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running)) @@ -186,11 +196,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc { idleWorkers := p.workers n := len(idleWorkers) - 1 if n < 0 { - if p.Running() >= p.Cap() { - waiting = true - } else { - atomic.AddInt32(&p.running, 1) - } + waiting = p.Running() >= p.Cap() } else { <-p.freeSignal w = idleWorkers[n] @@ -214,6 +220,7 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc { args: make(chan interface{}, 1), } w.run() + p.IncrRunning() } return w } diff --git a/worker.go b/worker.go index c0667fb..8d29a3a 100644 --- a/worker.go +++ b/worker.go @@ -23,7 +23,6 @@ package ants import ( - "sync/atomic" "time" ) @@ -47,7 +46,7 @@ func (w *Worker) run() { go func() { for f := range w.task { if f == nil { - atomic.AddInt32(&w.pool.running, -1) + w.pool.DecrRunning() return } f() diff --git a/worker_func.go b/worker_func.go index 86c7448..22b6069 100644 --- a/worker_func.go +++ b/worker_func.go @@ -23,7 +23,6 @@ package ants import ( - "sync/atomic" "time" ) @@ -47,7 +46,7 @@ func (w *WorkerWithFunc) run() { go func() { for args := range w.args { if args == nil { - atomic.AddInt32(&w.pool.running, -1) + w.pool.DecrRunning() return } w.pool.poolFunc(args)