Fixed #45 and Releasing v2 module

This commit is contained in:
Andy Pan 2019-08-25 14:25:09 +08:00
parent 2c15b60572
commit cbf8ddf048
9 changed files with 21 additions and 43 deletions

View File

@ -1,7 +1,6 @@
language: go language: go
go: go:
- "1.8.x"
- "1.9.x" - "1.9.x"
- "1.10.x" - "1.10.x"
- "1.11.x" - "1.11.x"

View File

@ -9,7 +9,7 @@ A goroutine pool for Go
<a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a> <a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a>
<br/> <br/>
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a> <a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a>
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square"></a> <a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square"></a>
<a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a> <a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a>
<a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a> <a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a>
</p> </p>
@ -53,7 +53,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
var sum int32 var sum int32
@ -114,7 +114,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
type Request struct { type Request struct {
@ -222,7 +222,7 @@ func WithPanicHandler(panicHandler func(interface{})) Option {
} }
``` ```
`ants.Options`contains all optional configurations of ants pool, which allow you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`method. `ants.Options`contains all optional configurations of ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in `NewPool`/`NewPoolWithFunc`method.
## Customize limited pool ## Customize limited pool
@ -269,14 +269,6 @@ All tasks submitted to `ants` pool will not be guaranteed to be addressed in ord
## Benchmarks ## Benchmarks
```
OS: macOS High Sierra
Processor: 2.7 GHz Intel Core i5
Memory: 8 GB 1867 MHz DDR3
Go Version: 1.9
```
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div> <div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
In that benchmark-picture, the first and second benchmarks performed test cases with 1M tasks and the rest of benchmarks performed test cases with 10M tasks, both in unlimited goroutines and `ants` pool, and the capacity of this `ants` goroutine-pool was limited to 50K. In that benchmark-picture, the first and second benchmarks performed test cases with 1M tasks and the rest of benchmarks performed test cases with 10M tasks, both in unlimited goroutines and `ants` pool, and the capacity of this `ants` goroutine-pool was limited to 50K.

View File

@ -9,7 +9,7 @@ A goroutine pool for Go
<a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a> <a title="Ants on Sourcegraph" target="_blank" href="https://sourcegraph.com/github.com/panjf2000/ants?badge"><img src="https://sourcegraph.com/github.com/panjf2000/ants/-/badge.svg?style=flat-square"></a>
<br/> <br/>
<a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a> <a title="" target="_blank" href="https://golangci.com/r/github.com/panjf2000/ants"><img src="https://golangci.com/badges/github.com/panjf2000/ants.svg"></a>
<a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square"></a> <a title="Godoc for ants" target="_blank" href="https://godoc.org/github.com/panjf2000/ants"><img src="https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square"></a>
<a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a> <a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"><img src="https://img.shields.io/github/release/panjf2000/ants.svg?style=flat-square"></a>
<a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a> <a title="License" target="_blank" href="https://opensource.org/licenses/mit-license.php"><img src="https://img.shields.io/aur/license/pac?style=flat-square"></a>
</p> </p>
@ -53,7 +53,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
var sum int32 var sum int32
@ -114,7 +114,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
type Request struct { type Request struct {
@ -269,18 +269,6 @@ pool.Release()
## Benchmarks ## Benchmarks
系统参数:
```
OS: macOS High Sierra
Processor: 2.7 GHz Intel Core i5
Memory: 8 GB 1867 MHz DDR3
Go Version: 1.9
```
<div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div> <div align="center"><img src="https://user-images.githubusercontent.com/7496278/51515466-c7ce9e00-1e4e-11e9-89c4-bd3785b3c667.png"/></div>
上图中的前两个 benchmark 测试结果是基于100w 任务量的条件,剩下的几个是基于 1000w 任务量的测试结果,`ants`的默认池容量是 5w。 上图中的前两个 benchmark 测试结果是基于100w 任务量的条件,剩下的几个是基于 1000w 任务量的测试结果,`ants`的默认池容量是 5w。

View File

@ -28,7 +28,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
const ( const (

View File

@ -29,7 +29,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
const ( const (

View File

@ -28,7 +28,7 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants/v2"
) )
var sum int32 var sum int32

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/panjf2000/ants/v2
go 1.12

12
pool.go
View File

@ -196,15 +196,11 @@ func (p *Pool) Cap() int {
} }
// Tune changes the capacity of this pool. // Tune changes the capacity of this pool.
func (p *Pool) Tune(size uint) { func (p *Pool) Tune(size int) {
if p.Cap() == int(size) { if p.Cap() == size {
return return
} }
atomic.StoreInt32(&p.capacity, int32(size)) atomic.StoreInt32(&p.capacity, int32(size))
diff := p.Running() - int(size)
for i := 0; i < diff; i++ {
p.retrieveWorker().task <- nil
}
} }
// Release Closes this pool. // Release Closes this pool.
@ -292,12 +288,14 @@ func (p *Pool) retrieveWorker() *goWorker {
// revertWorker puts a worker back into free pool, recycling the goroutines. // revertWorker puts a worker back into free pool, recycling the goroutines.
func (p *Pool) revertWorker(worker *goWorker) bool { func (p *Pool) revertWorker(worker *goWorker) bool {
if atomic.LoadInt32(&p.release) == CLOSED { if atomic.LoadInt32(&p.release) == CLOSED || p.Running() > p.Cap() {
worker.task <- nil
return false return false
} }
worker.recycleTime = time.Now() worker.recycleTime = time.Now()
p.lock.Lock() p.lock.Lock()
p.workers = append(p.workers, worker) p.workers = append(p.workers, worker)
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
p.cond.Signal() p.cond.Signal()
p.lock.Unlock() p.lock.Unlock()

View File

@ -210,10 +210,6 @@ func (p *PoolWithFunc) Tune(size int) {
return return
} }
atomic.StoreInt32(&p.capacity, int32(size)) atomic.StoreInt32(&p.capacity, int32(size))
diff := p.Running() - size
for i := 0; i < diff; i++ {
p.retrieveWorker().args <- nil
}
} }
// Release Closed this pool. // Release Closed this pool.
@ -301,12 +297,14 @@ func (p *PoolWithFunc) retrieveWorker() *goWorkerWithFunc {
// revertWorker puts a worker back into free pool, recycling the goroutines. // revertWorker puts a worker back into free pool, recycling the goroutines.
func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool { func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
if atomic.LoadInt32(&p.release) == CLOSED { if atomic.LoadInt32(&p.release) == CLOSED || p.Running() > p.Cap() {
worker.args <- nil
return false return false
} }
worker.recycleTime = time.Now() worker.recycleTime = time.Now()
p.lock.Lock() p.lock.Lock()
p.workers = append(p.workers, worker) p.workers = append(p.workers, worker)
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
p.cond.Signal() p.cond.Signal()
p.lock.Unlock() p.lock.Unlock()